Solved

C# connect to database

Posted on 2014-09-23
5
119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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