?
Solved

C# connect to database

Posted on 2014-09-23
5
Medium Priority
?
121 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

741 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