测试连接

 

//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();
}

posted @ 2016-02-20 11:37  Carry7938  阅读(189)  评论(0编辑  收藏  举报