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();
}
精彩评论