0

I have a C# Console Application that is essentially a long batch process that processes data on a nightly basis across many different databases. What is the proper or preferred way to test basic database connectivity at the beginning of this type of program? I ran into the issue of having an expired database password for one of my connections that was caught by exception handling but I want to test for basic connectivity at the very beginning.

Would a simple SELECT query suffice or is there a more efficient way of doing this for numerous databases?

4 Answers 4

3

IMHO the simplest way is trying to connect to database and, if you have a failure, you give up.
As you're running a night batch, it's not important to understand immediately the reason and solve it.
So something like this

using(SqlConnection conn = new SqlConnection(connectionString))
{
    try
    {
        conn.Open();
        // Do what you please here        
    }
    catch (Exception ex)
    {
        // Write error to file
        File.Append(..., 
            DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " " + 
            ex.Message);
    }
    finally 
    { 
        conn.Close();
    } 
}

Next morning you can check file for errors...

Sign up to request clarification or add additional context in comments.

5 Comments

@OlivierJacot-Descombes: I don't understand... what's wrong with my answer?
The answer looks fine to me. Side-note: Do you need to use the conn.Close() when enclosed in a using statement?
@ShawnH.: I'm not sure, I should test it. I was wondering right when posting answer, but because of I'm not sure... well, it's better to be sure it's closed!! :)
@OlivierJacot-Descombes: I was editing my answer, but I did it six minutes before you posted your comment! ;)
Never mind. For some reason it's the version a saw when I posted my comment. I deleted my comments.
0
'Connection.open` 

is the simple way to determine if you can connect to db or not.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
}

If you get a SqlException with number 18487 or 18488 it indicates the password has been changed.

Connection.changePassword

Comments

0

You don't need to run any query. If you use SqlConnection passing the connection string, you can just try to Open() the connection and you'll get an exception if you cannot connect Something like:

try
{
    var cnn = new SqlConnection(connectionString);
    cnn.Open();
}
catch
{
   // connection failed, do something
}

Comments

0

Opening (and then closing) a connection should be sufficient to test the password. however, this does not tell you , if a db-user has permissions to access specific tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.