Solved

C# connect to database

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now