4

I want to test the database can be connected in fast seconds,for example i want to test it in 3 seconds. I wrote these codes.

 bool IsCanConnectioned = false;
            string connStr = connectionString + ";Connection Timeout=3000";
            SqlConnection sqlConnection = new SqlConnection(connStr);
            try
            {
                sqlConnection.Open();
                IsCanConnectioned = true;
            }
            catch
            {

                IsCanConnectioned = false;
            }
            finally
            {

                sqlConnection.Close();
                sqlConnection.Dispose();
            }
              return IsCanConnectioned;

I broken my network to database for this test, but it's no working to set the connection timeout 3000 or 3. It will throw exception after a long time about more than 15 seconds. (I guess it's because the network timeout) I want to test it in 3 seconds. so is there a good way to do this.

may be It could start a new thread to connect the database,and I wait for the thread 3 seconds,when it's no response or return false,I said the database couldn't be connected,but is there a better way?

elsonwx
  • 1,431
  • 3
  • 16
  • 26
  • 1
    You could eradicate the boolean variable all together and just return true/false, the finally block will still be executed to clear up resources. – horHAY Jun 05 '15 at 07:35
  • possible duplicate of [Sql connection waits 15 seconds despite 3 seconds timeout in connection string](http://stackoverflow.com/questions/3114051/sql-connection-waits-15-seconds-despite-3-seconds-timeout-in-connection-string) – horHAY Jun 05 '15 at 07:36
  • yes ,the IsCanConnectioned should be changed,haha – elsonwx Jun 05 '15 at 07:45
  • I don't understand what the point of that test is? There can be many different factors that will change the test results. e.g. Network load, if you preform the test at your network peak, you will receive a different result. Seems kind of pointless in my opinion. – Jamie Rees Jun 05 '15 at 07:52
  • http://stackoverflow.com/questions/3619347/how-to-make-sqlconnection-timeout-more-quickly – elsonwx Jun 05 '15 at 08:21
  • @JamieRees it could be useful because it fails at network peak, if it then goes on to do something that could be better delayed until network use was lighter. – Jon Hanna Jun 05 '15 at 11:29

2 Answers2

1

You say you broke the network for this test but ConnectionTimeout applies when you can reach the server on which SqlServer is installed. Otherwise TCP timeout get involved, which by default is 15sec.

If you want to timeout even if the network is down you should setup a tcp connection and try to connect. See this answer on how to do it.

Community
  • 1
  • 1
Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50
  • I want to return the result in 3 seconds Whether the network reasons or the database reasons。 – elsonwx Jun 05 '15 at 07:52
  • this question helped me,haha http://stackoverflow.com/questions/3619347/how-to-make-sqlconnection-timeout-more-quickly – elsonwx Jun 05 '15 at 08:21
0

I think you should check sqlConnection.State, which can have any value from state enum.

Also To set the time out For TCP Time out have a look at How to make SqlConnection timeout more quickly

Community
  • 1
  • 1
Dreamweaver
  • 1,328
  • 11
  • 21