title: C# Retry Sql Query if Timeout Failed
body: <p>at time my query times out and cause a failure.  What is the best way to retry to execute a query? </p>
  
  <p>I validate that the connection is open before executing the query.  However, due to the server load at any given time, it may take &lt;1 min to 5+ minutes.  I thought about extending the CommandTimeout but I do not believe that is really the way to go. </p>
  
  <p>Here is my sql query code.  Thanks for all the assistance.</p>
  
  <pre><code>    private static void ExecuteQuery(string connectionString, string query)
      {
          SqlConnection connection = new SqlConnection(connectionString);
          DataTable output = new DataTable();
  
          try
          {
              //create new SqlAdataAdapter
              SqlDataAdapter command = new SqlDataAdapter {SelectCommand = new SqlCommand(query, connection)};
  
              //connect to Sqldb
              connection.Open();
  
              //validate connection to database before executing query
              if (connection.State != ConnectionState.Open) return;
              Console.WriteLine("Connection successful\nExecuting query...");
  
              //set connection timeout
              command.SelectCommand.CommandTimeout = 200;
  
              //create new dataSet in order to input output of query to
              DataSet dataSet = new DataSet();
  
              //fill the dataSet
              command.Fill(dataSet, "capacity");
              DataTable dtTable1 = dataSet.Tables["capacity"];
  
              Console.WriteLine("There are " + dtTable1.Rows.Count + " clusters within the capacity anlaysis.");
  
              output = dtTable1;
          }
          catch (Exception e)
          {
              Console.WriteLine("Unable to execute capacity (all records) query due to {0}", e.Message);
          }
          finally
          {
              connection.Close();
  
              Declarations.NumOfClusters = output.Rows.Count;
              Declarations.finalIssues = Issues(output, 2m, 20, true);
  
              Console.WriteLine("\n---------------Successfully Created Capacity DataSet---------------\n");
          }
      }
  </code></pre>