zl程序教程

您现在的位置是:首页 >  后端

当前栏目

ASP.NET连接SQL数据库的简单实例代码

2023-06-13 09:15:03 时间

复制代码代码如下:


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

namespaceExample01
{
///
///WebForm1的摘要说明。
///
publicclassWebForm1:System.Web.UI.Page
{
privatevoidPage_Load(objectsender,System.EventArgse)
{
//在此处放置用户代码以初始化页面
Response.Write("第一个连接数据库的实例");
SqlConnectionmyConnection=newSqlConnection("server=localhost;uid=sa;pwd=sa");
try
{
myConnection.Open();//打开数据库链接
Response.Write("链接成功!");
}
catch
{
Response.Write("链接失败!");
}
}

#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///
privatevoidInitializeComponent()
{
this.Load=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}