c#

位置:IT落伍者 >> c# >> 浏览文章

ADO.Net 连接数据库示例


发布日期:2021年02月07日
 
ADO.Net 连接数据库示例

连接本地SQL Server数据库学习示例

配置nfig

<connectionStrings>

<add name=We_CMS connectionString=Data Source=ZhangNanPC; Initial Catalog=We_CMS ; Integrated Security=SSPI/>

</connectionStrings>

写一个测试连接的web form

<body>

<form id=form runat=server>

<div>

<asp:Label runat=server ID=lblInfo></asp:Label>

</div>

</form>

</body>

protected void Page_Load(object sender EventArgs e)

{

string connectionString = WebConfigurationManagerConnectionStrings[We_CMS]ConnectionString;

SqlConnection con = new SqlConnection(connectionString)

try

{

conOpen()

lblInfoText = <b>Version : </b>+conServerVersion;

lblInfoText += <br /><b>Connection Is:</b> + conStateToString()

}

catch (Exception err)

{

lblInfoText = Error reading the database + errMessage;

}

finally

{

conClose()

lblInfoText += <br /><b> Now Connection Is : </b> + conStateToString()

}

}

测试结果如下

Version :

Connection Is:Open

Now Connection Is : Closed

               

上一篇:C#中用钩子(hook)实现的屏幕键盘

下一篇:Visual C#程序设计技巧小结