开发者

How do I set the timeout when filling a temporary table based on a complex query?

开发者 https://www.devze.com 2022-12-08 01:40 出处:网络
How do I set the command timeout in the following situation?Just to clarify, I have already set the connection timeout in the Connection String, but I also need to set the command timeout because I wa

How do I set the command timeout in the following situation? Just to clarify, I have already set the connection timeout in the Connection String, but I also need to set the command timeout because I want the query to be able to run 5 minutes if it needs to, but it times out in less than a few minutes.

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

 SqlConnection ReportConnect = new SqlConnection(ConnectionString);

 ReportConnect.Open();

 DataSet tempDataset = new DataSet();

 SqlDataAdapter da开发者_Python百科 = new SqlDataAdapter(reportQuery, ReportConnect);

 da.Fill(tempDataset);


You can create a SqlCommand set the CommandTimeout property on the command and then pass that to the constructor of the data adapter. Somthing like this:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号