ASP. NET simple instance code to connect to the SQL database

  • 2020-06-15 08:06:10
  • OfStack


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
{
///
/// WebForm1  The summary description of. 
///
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 1 Two instances that connect to the database ");
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! ");
}
}
#region Web  Code generated by the forms designer 
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:  The call is  ASP.NET Web  Required by the Forms designer. 
//
InitializeComponent();
base.OnInit(e);
}
///
///  The designer supports the required methods  -  Do not modify using the code editor 
///  The contents of this method. 
///
private void InitializeComponent()
{
this.Load = new System.EventHandler(this.Page_Load);
}
#endregion
}
} 


Related articles: