C# 增加SQL命令的命令超时
我有点小问题,希望有人能给我一些建议。我正在运行一个SQL命令,但是由于有大量数据,该命令似乎需要大约2分钟才能返回数据。但是默认的连接时间是30秒,我如何增加它,并将其应用于这个命令C# 增加SQL命令的命令超时,c#,sql,ado.net,connection-timeout,C#,Sql,Ado.net,Connection Timeout,我有点小问题,希望有人能给我一些建议。我正在运行一个SQL命令,但是由于有大量数据,该命令似乎需要大约2分钟才能返回数据。但是默认的连接时间是30秒,我如何增加它,并将其应用于这个命令 public static DataTable runtotals(string AssetNumberV, string AssetNumber1V) { DataTable dtGetruntotals; try { dtGetruntotals = new Data
public static DataTable runtotals(string AssetNumberV, string AssetNumber1V)
{
DataTable dtGetruntotals;
try
{
dtGetruntotals = new DataTable("Getruntotals");
//SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 6);
//AssetNumber.Value = AssetNumberV;
SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 10);
AssetNumber.Value = AssetNumberV;
SqlParameter AssetNumber1 = new SqlParameter("@AssetNumber1", SqlDbType.VarChar, 10);
AssetNumber1.Value = AssetNumber1V;
SqlCommand scGetruntotals = new SqlCommand("EXEC spRunTotals @AssetNumber,@AssetNumber1 ", DataAccess.AssetConnection);
// scGetruntotals.Parameters.Add(AssetNumber);
scGetruntotals.Parameters.Add(AssetNumber);
scGetruntotals.Parameters.Add(AssetNumber1);
SqlDataAdapter sdaGetruntotals = new SqlDataAdapter();
sdaGetruntotals.SelectCommand = scGetruntotals;
sdaGetruntotals.Fill(dtGetruntotals);
return dtGetruntotals;
}
catch (Exception ex)
{
MessageBox.Show("Error Retriving totals Details: Processed with this error:" + ex.Message);
return null;
}
}
添加
SqlCommand
的超时。请注意时间是秒
// Setting command timeout to 1 second
scGetruntotals.CommandTimeout = 1;
由于数据量很大,因此该命令大约需要2分钟才能返回数据
可能是因为设计不好。考虑在这里使用分页。
默认连接时间为30秒,如何增加
由于命令面临超时,因此需要增加命令的超时时间。您可以像这样在命令中指定它
// Setting command timeout to 2 minutes
scGetruntotals.CommandTimeout = 120;
由于响应需要2分钟,您可以通过添加以下代码将超时时间增加到3分钟
scGetruntotals.CommandTimeout = 180;
注意:参数值以秒为单位。不建议将CommandTimeout设置为120。尝试使用上面提到的分页。将CommandTimeout设置为30视为正常。任何比这更糟糕的方法,这通常会导致一些错误的实施。现在世界正在以毫秒的速度运行。将命令超时设置为2分钟
scGetruntotals.CommandTimeout = 120;
但是,您可以优化存储过程以减少时间!
像
- 移除马槽或while等
- 使用分页
- 使用#tentable和@variableTable
- 优化联接表