ASP. NET database connection code

Source: Internet
Author: User

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HTMLControls;
Using System. Data. SqlClient;

Namespace Example01
{
///
/// Summary of WebForm1.
///
Public class WebForm1: System. Web. UI. Page
{
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
Response. Write ("

The first database connection instance

");
SqlConnection myConnection = new SqlConnection ("server = localhost; uid = sa; pwd = sa ");
Try
{
MyConnection. Open (); // Open the database link
Response. Write ("link successful! ");
}
Catch
{
Response. Write ("link failed! ");
}
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
Private void InitializeComponent ()
{
This. Load = new System. EventHandler (this. Page_Load );
}
# Endregion
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.