Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase timeout of DataAdapter to 3 min?

When i execute a query with the following method, i get a timeout.

So my question is: how can i set the timeout to 180 seconds?

I'm using the connection to fill the dataSet with the queryresult.

 internal static DataSet executeQuery(string queryString)
 {
     // #connection
     DataSet dataSet = new DataSet();
     string connectionString = Connection.connectionStringSQL01NavProvider();
     OleDbConnection connection = new OleDbConnection(connectionString);
     OleDbDataAdapter adapter = new OleDbDataAdapter(queryString, connectionString);

     // Open the connection and fill the DataSet. 
     connection.Open();
     try
     {
         adapter.Fill(dataSet);
         DataTable dt = new DataTable();
         dt = dataSet.Tables[0];
         DataRow dr;
         try
         {
             dr = dt.Rows[0];
         }
         catch 
         {

         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         System.Windows.Forms.MessageBox.Show("Error executeQuery().! " + ex.Message);
     }

     return dataSet;
 }
like image 252
JP..t Avatar asked Jun 18 '15 08:06

JP..t


People also ask

Which is faster DataAdapter vs DataReader?

Answers. Datareaders are fast compare to DataAdapters/DataSets because of the following reason. DataReader offers better performance because it avoids the performance and memory overhead associated with the creation of the DataSet.

What does DataAdapter fill do?

The Fill method of the DataAdapter is used to populate a DataSet with the results of the SelectCommand of the DataAdapter . Fill takes as its arguments a DataSet to be populated, and a DataTable object, or the name of the DataTable to be filled with the rows returned from the SelectCommand .

What is SQL Server DataAdapter?

The DataAdapter uses the Connection object of the Microsoft SqlClient Data Provider for SQL Server to connect to a data source, and it uses Command objects to retrieve data from and resolve changes to the data source.


1 Answers

You can set the CommandTimeout of the SelectCommand:

adapter.SelectCommand.CommandTimeout = 180; // default is 30 seconds

If you can't establish a connection to the database and you also want to increase that timeout, you have to do that in the connection-string, for example(default is 15 seconds):

"Data Source=(local);Connection Timeout=30;Initial Catalog=AdventureWorks; Integrated Security=SSPI;"

Note that you should use the using-statement for your connection and other objects implementing IDisposable like the OleDbDataAdapter. On that way you ensure that all unmanaged resources are disposed properly:

internal static DataSet executeQuery(string queryString)
{
    DataSet dataSet = new DataSet();
    string connectionString = Connection.connectionStringSQL01NavProvider();
    using (var connection = new OleDbConnection(connectionString))
    using(var adapter = new OleDbDataAdapter(queryString, connectionString))
    {
        try
        {
            adapter.Fill(dataSet); // you dont need to open/close the connection with Fill
        } catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            System.Windows.Forms.MessageBox.Show("Error executeQuery().! " + ex.Message);
        }
    }

    return dataSet;
}
like image 86
Tim Schmelter Avatar answered Sep 17 '22 20:09

Tim Schmelter