Solved

C# connect to database

Posted on 2014-09-23
5
113 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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