How to correct SqlConnection.Timeout value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JSan
    New Member
    • Jun 2010
    • 4

    How to correct SqlConnection.Timeout value?

    Hi, i got a bug on a C# application, the problem is that i have a static class to manage all transactions with a database, and the application should try for itself to connect to defined SQL Server IP's. Everything is fine except that when i try to connect to any of the IP's the timeout specified in the Connection string is ignored, instead takes the default value (30 secs) and this is a pain. If the app can“t connect to any of the specified IPs then ask the user for the IP and makes another attempt to connect, this time using an overloaded function that takes the IP addr as parameter, and the weird is that in this attempt the Timeout works as specified in the connection string. Anyone has an idea about how to solve this?

    Code:
    foreach (string IP in SQL_IPs)
                {
                    string ConnectionString = CSPart1 + IP + CSPart2;
                    try
                    {
                        SQLCon = new SqlConnection(ConnectionString);
                        
                        SQLCon.Open();
                        if (SQLCon.State == ConnectionState.Open)
                        {
                            BuildArrays();
                            return true;
                        }
                    }
                    catch (SqlException sqlex)
                    {
                        Console.WriteLine("===========================================");
                        Console.WriteLine(sqlex.Message);
                        Console.WriteLine(sqlex.StackTrace);
                        Console.WriteLine(sqlex.Source);
                        Console.WriteLine("===========================================");
                        continue;
                    }
                }
                return false;
    ConnectionStrin g Used:
    Code:
    Initial Catalog=DB1; Data Source=172.16.1.5;User ID=admin; Password=admin; Connection Timeout=3
    Thanks in advance
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Initial Catalog=DB1; Data Source=172.16.1 .5;User ID=admin; Password=admin; Connection Timeout=3
    The keyword is Timeout and not Connection Timeout

    and you cannot specify timeout as 3 as it can be specified in milliseconds

    Comment

    • JSan
      New Member
      • Jun 2010
      • 4

      #3
      Thanks for the answer, but i found a workaround using a thread then join the thread in less of a specified timeout, because i was trying to control the TCP timeout (if one of the servers is down try the next) Thanks anyway

      Comment

      Working...