如何将DataAdapter的超时时间增加到3分钟? [英] How to increase timeout of DataAdapter to 3 min?

查看:106
本文介绍了如何将DataAdapter的超时时间增加到3分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下方法执行查询时,出现超时。

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

所以我的问题是:如何将超时设置为180秒?

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

我正在使用

 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;
 }


推荐答案

您可以设置 ).aspx rel = noreferrer> SelectCommand

You can set the CommandTimeout of the SelectCommand:

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

如果您无法建立与数据库的连接,并且您还想要要增加超时时间,您必须在连接字符串中执行此操作,例如(默认值为15秒):

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;"






请注意,您应使用使用语句进行连接以及实现 IDisposable 的其他对象,例如 OleDbDataAdapter 。这样一来,您可以确保正确处置所有非托管资源:


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;
}

这篇关于如何将DataAdapter的超时时间增加到3分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆