連接本地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
From:http://tw.wingwit.com/Article/program/net/201311/11353.html