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

时间:2013-06-24 16:46:03

标签: c# asp.net sql-server-2008 ado.net

以下是一段不好的代码片段:

    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;
    }       
    }
    }

发生的事情是它挂了大约2分钟,然后最终得到一个超时错误。但是,如果我在SQL Server Management Studio中运行存储过程,并使用相同的确切参数,则数据会立即返回。

“挂起”发生在“da.Fill(ds);”线。有人有主意吗?顺便说一下,这是.NET 2.0

1 个答案:

答案 0 :(得分:2)

可能重复的问题?

SqlDataAdapter.Fill() Timeout - Underlying Sproc Returns Quickly

WITH(NOLOCK)语句中考虑SELECT