1

以下情况如何设置命令超时?澄清一下,我已经在连接字符串中设置了连接超时,但我还需要设置命令超时,因为我希望查询能够在需要时运行 5 分钟,但它会在不到一分钟的时间内超时一会儿。

 String reportQuery = @"  complicated query returning many rows    ";

 SqlConnection ReportConnect = new SqlConnection(ConnectionString);

 ReportConnect.Open();

 DataSet tempDataset = new DataSet();

 SqlDataAdapter da = new SqlDataAdapter(reportQuery, ReportConnect);

 da.Fill(tempDataset);
4

1 回答 1

8

您可以在命令上创建一个属性SqlCommandCommandTimeout,然后将其传递给数据适配器的构造函数。像这样的东西:

String reportQuery = @"  complicated query returning many rows    ";
SqlConnection ReportConnect = new SqlConnection(ConnectionString);
ReportConnect.Open();
SqlCommand command = new SqlCommand(reportQuery, ReportConnect);
command.CommandTimeout = 300; //5 mins
DataSet tempDataset = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(tempDataset);
于 2009-10-07T05:12:36.863 回答