views:

37

answers:

4

I need to check whether a SQL Server database is up and running properly using .NET windows application.

+3  A: 

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;
}
Johannes Rudolph
+1 exactly - just try to open the connection and be prepared to handle any exceptions that might happen.
marc_s
A: 

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.

Dean Harding
A: 

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();
}
rahul
you could also put the SqlConnection con =.... stuff into a `using(SqlConnection con = new .....) { } ` and thus save yourself from having to write a `finally` block - .NET will take care of that
marc_s
A: 

Can someone help me how to do the same in VB6 rather than .NET? My aim is to send out an email to the network support/DBA when the database is not responsive. Any help?

Kanthi K