开发者

To check whether the SQL server DB is working properly or not

开发者 https://www.devze.com 2022-12-26 00:09 出处:网络
I need to check whether a SQL Server database is开发者_运维知识库 up and running properly using .NET windows application.The simplest thing that allows you to do this is trying to open up a connection

I need to check whether a SQL Server database is开发者_运维知识库 up and running properly using .NET windows application.


The simplest thing that allows you to do this is trying to open up a connection. If it fails to open up the connection, your database (or network) is not working correctly.

try 
{
  SqlConnection con = new SqlConnection("YOUR_CONNECTIONSTRING");
  con.Open();
  con.Close();
}
catch (Exception)
{
  throw;
}


You really need to provide more information than this, but the simplest way would be to attempt to run a query against it. If the query returns results as you'd expect, then the server is up & running. If not, then something is wrong.


You can use the SqlConnection.Open method to open a database connection, and check for any exception.

SqlConnection con = new SqlConnection("connection string");

try 
{
    con.Open();
}
catch (Exception)
{
    // handle your exception here
}
finally
{  
    con.Close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消