码迷,mamicode.com
首页 > 其他好文 > 详细

测试连接

时间:2016-02-20 13:10:57      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

 

//Create the Connection object
string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
try
{
//Try to open the connection.
con.Open();
lblInfo.Text = "<b>Server Version:</b>" + con.ServerVersion;
lblInfo.Text += "<br/><b>Connection Is:</b>" + con.State.ToString();

}
catch(Exception err)
{
//handle an error by displaying the infromation.
lblInfo.Text = "Error reading the datebase." + err.Message;
}
finally
{
//Either way ,make sure the connection is properly closed
// even if the connection wasn‘t opened successfully
//calling close() won‘t cause an error.
con.Close();
lblInfo.Text = "<br/><b>Now Connection Is:</b>" + con.State.ToString();
}

测试连接

标签:

原文地址:http://www.cnblogs.com/CarryLi7938/p/5202960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!