【问题标题】:How to increase the query time?如何增加查询时间?
【发布时间】:2011-05-09 04:49:48
【问题描述】:

在使用 C#.NET、Windows Forms 的程序中是否有可能增加查询执行超时时间?

在我的程序中,我正在循环执行一个查询。在前两次迭代中,查询每次在 10 秒内执行。在第三次迭代中,查询完成大约需要 40-50 秒,现在我无法从该查询中提取数据,因为引发了超时异常并且执行流程转到 catch 块,其中对应的显示错误信息。

是否有可能在 Windows 应用程序中使用 C#.NET 执行耗时超过 30 秒的查询?

下面是我的代码:

try
{
    tab = "dbo.basic_sp_mst";
    error1 = "" + site + "." + orgname + "";
    ls = "" + site + "." + site + "." + tab + "";
    SqlDataAdapter da2 = new SqlDataAdapter("select * from " + ls + " where (last_updated_date between(getdate()-3) and (getdate()-1)) order by last_updated_date,bsp_item_cd", con);
    DataSet ds2 = new DataSet();
    da2.Fill(dt5);
    dt6 = dt5;

    SqlDataAdapter daa = new SqlDataAdapter("select a. bsp_item_cd ,a.bsp_mrp,a.last_updated_date,b.bsp_item_cd ,b.bsp_mrp,b.last_updated_date,c.name from basic_sp_mst a," + ls + " b,sys.servers c where  a.bsp_item_cd =  b.bsp_item_cd and a.bsp_mrp<> b.bsp_mrp and a.bsp_org_cd=" + s + " and c.name<>'001' and c.name='" + site + "'", con);......................(this is the main query) if this query is taking more than 30sec it is going to catch block.......

    DataSet dss = new DataSet();
    daa.Fill(dt7);
    dt8 = dt7;
}
catch (Exception ex)
{
    msg2 = " query time out error at store id:" + error1 + "";
    dataGridView8.Rows.Add(msg2);
}

【问题讨论】:

    标签: sql-server-2008 c#-4.0


    【解决方案1】:

    只需增加 SqlDataAdapter 的 CommandTimeout 属性即可。

    // Set timeout to 2 minutes
    daa.SelectCommand.CommandTimeout=120;
    

    此命令位于 new SqlDataAdapter 定义之后和对 Fill 的调用之前。

    另外,检查表是否在Select 命令的Where 部分的列上具有索引,以尝试减少查询时间。

    您也可以use the MS SQL Server Profiler 接收有关可以提高性能的索引的建议。

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      相关资源
      最近更新 更多