连接本地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