Solved

C# connect to database

Posted on 2014-09-23
5
114 Views
Last Modified: 2014-10-08
I have a C# application. I noticed if the app cannot make a connection to the database the program hangs but does eventually come back with an error that it cannot connect. But the time it takes to respond t hat there is no connection is quite awhile and you may think that it has hung.

Is t here any way to quickly check if t here is a connection and if there is no service it reports it quickly.
0
Comment
Question by:Idarac
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40340226
It might be different depending on how you connect to the database, but for most programmers, setting the ConnectionTimeOut property of the SqlConnection object to the number of seconds that you want to wait if sufficient. You can set that through the property or through a the connectionstring.

SqlConnection con = New SqlConnection("yourConnectionString");
con.ConnectionTimeOut=5;
con.Open();
0
 
LVL 1

Author Comment

by:Idarac
ID: 40340271
Thank you James

Do you know what the default is if you do not set the timeout property?
0
 
LVL 40
ID: 40340340
According to the documentation, the default is 15 seconds. I never measured. It seems longer than that, but human beings are impatient when a computer seems to be slow, so it might be right.
0
 
LVL 8

Expert Comment

by:johny_bravo1
ID: 40363076
JamesBurger is right.

MySql Offers a Connection.Ping() method that returns true or false even if you haven't called Connection.Open() before.

For Sql server

If connection string is wrong this will throw an exception

public static class Extension
{
    public static void TestDbConnection(this DbConnection connection)
    {
        var builder = new DbConnectionStringBuilder
            {
                ConnectionString = connection.ConnectionString
            };
    }
}
0
 
LVL 1

Author Closing Comment

by:Idarac
ID: 40369752
Setting the timeout worked thank you.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question