简体   繁体   English

ADO.NET在尝试运行存储过程时超时

[英]ADO.NET timing out while trying to run stored procedure

Here is a snippet of the code that is bad: 这是不好的代码片段:

    string storedProcedure = "sp3111Commissions";
    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) {
    string ConnectionString = CMSLayers.DataLayer.Universal.GetConnectionString();
    ConnectionString = 
    using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection()) {
    //Response.Write(EndDate2.ToString());
    //Response.End();
            conn.ConnectionString = ConnectionString;
    //Response.Write(ConnectionString); Response.End(); return null;
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Connection = conn;
    cmd.CommandText = storedProcedure;
    cmd.Parameters.Add("@ShopID", System.Data.SqlDbType.Int);
    cmd.Parameters["@ShopID"].Value = ShopID;
    cmd.Parameters.Add("@StartDate", System.Data.SqlDbType.DateTime);
    cmd.Parameters["@StartDate"].Value = StartDate;
    cmd.Parameters.Add("@EndDate", System.Data.SqlDbType.DateTime);
    cmd.Parameters["@EndDate"].Value = EndDate;
    cmd.Parameters.Add("@StartDate2", System.Data.SqlDbType.DateTime);
    cmd.Parameters["@StartDate2"].Value = StartDate2;
    cmd.Parameters.Add("@EndDate2", System.Data.SqlDbType.DateTime);
    cmd.Parameters["@EndDate2"].Value = EndDate2;
    cmd.CommandTimeout = 1380;

    try
    {
        System.Data.DataSet ds;
        using (System.Data.SqlClient.SqlDataAdapter da= new System.Data.SqlClient.SqlDataAdapter()) {
        System.Data.DataTable Table = null;

        conn.Open();

        da.SelectCommand = cmd;

        using (ds = new System.Data.DataSet()) {

        da.Fill(ds);

        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                Table = ds.Tables[0];
            }
        }
        conn.Close();
        return Table;
        }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }       
    }
    }

What's happening is it hanging for about 2 minutes, then finally getting a time out error. 发生的事情是它挂了大约2分钟,然后最终出现超时错误。 However, if I run the stored procedure in SQL Server Management Studio, with the same exact parameters, the data comes back literally instantly. 但是,如果我在SQL Server Management Studio中使用相同的确切参数运行存储过程,则数据立即会立即返回。

The "hang" is occurring at the "da.Fill(ds);" “挂起”发生在“ da.Fill(ds);”处。 line. 线。 Anyone have any ideas? 有人有想法么? This is .NET 2.0, by the way 顺便说一下,这是.NET 2.0

Possible duplicate question? 可能重复的问题?

SqlDataAdapter.Fill() Timeout - Underlying Sproc Returns Quickly SqlDataAdapter.Fill()超时-基本Sproc快速返回

Consider WITH(NOLOCK) in your SELECT statements. SELECT语句中考虑WITH(NOLOCK)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM