none
sql connection in c# command timeout issue

    Question

  • Hi, I am querying a table from c# and explicitly setting CommandTimeout = 0 and still run into the below error. I have used similar code in other applications in the same environment and sql connectionstring also same and never ran into any connection timeout.

    Error: System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

    Code:

    static void Main(string[] args)
    {
     string cString = @"Data source = server;Integrated Security = true;Initial Catalog = database";
    
     using(SqlConnection sc = new SqlConnection(cString))
     {
      try
      {
       SqlCommand cmd = new SqlCommand("select * from table", sc);
       cmd.CommandTimeout = 0;
       Console.WriteLine("CommandTimeout: {0}", cmd.CommandTimeout);
       sc.Open();
       SqlDataReader r = cmd.ExecuteReader();
    
       while (r.Read())
       Console.WriteLine("{0}: {1}", r[0].ToString(), r[1].ToString());
       sc.Close();
      } 
      catch (SqlException se)  
      {
       Console.WriteLine(se);
      }
     }
    }

    Thank you in advance.


    SQLEnthusiast

    Thursday, April 27, 2017 6:16 PM

All replies