通过ado.net从C#调用SQL Server存储过程超时

时间:2011-12-05 14:16:35

标签: c# sql-server-2005 ado.net .net-2.0

我在SQL Server中创建了一个存储过程,它直接从Management Studio运行。

从代码调用存储过程时,使用完全相同的参数,它会超时。

存储过程接受许多参数,大多数参数都是NULL作为默认值。我尝试过多种方式为SqlCommand对象分配参数:

  • 不添加空值参数
  • 添加空值的参数并传递为值DBNull.Value
  • 使用SQLCommandBuilder.DeriveParameters并仅指定非空值

Sp其中包含2个datetime个参数。当这些日期的范围很小时,查询会运行,但是对于较大的范围(3个月左右),当我尝试执行ExecuteReader方法时,我得到SqlException - Timeout expired。要返回的行不多(大约3500)。

为了查看流程发生了什么,我在ExecuteReader方法开始运行时运行以下查询

SELECT st.text AS [SQL Text],
       w.session_id, 
       w.wait_duration_ms,
       w.wait_type, w.resource_address, 
       w.blocking_session_id, 
       w.resource_description 
FROM sys.dm_os_waiting_tasks AS w
INNER JOIN sys.dm_exec_connections AS c ON w.session_id = c.session_id 
CROSS APPLY (SELECT * FROM sys.dm_exec_sql_text(c.most_recent_sql_handle)) AS st 
WHERE w.session_id > 50
  AND w.wait_duration_ms > 0

我得到如下结果。这是正常的吗?

SQL Text    session_id  wait_duration_ms    wait_type   resource_address    blocking_session_id resource_description
CREATE PROCEDURE [dbo].[GetVoucherJobs]...  64  23993   CXPACKET    0x000000008012A870  64  exchangeEvent id=port801283d0 nodeId=0
CREATE PROCEDURE [dbo].[GetVoucherJobs]...  64  15  IO_COMPLETION   NULL    NULL    NULL
CREATE PROCEDURE [dbo].[GetVoucherJobs]...  64  15  IO_COMPLETION   NULL    NULL    NULL
CREATE PROCEDURE [dbo].[GetVoucherJobs]...  64  2121    CXPACKET    0x000000008012BE60  64  exchangeEvent id=port801283d0 nodeId=0

如果不是调用存储过程,而是通过将CommandText设置为sp立即运行的主体来运行查询。这是我的存储过程:

CREATE PROCEDURE [dbo].[GetVoucherJobs] 
    @sUsrClientCode NVARCHAR(50),
    @dFr DATETIME = NULL,
    @dTo DATETIME = NULL,
    @sVchSerial NVARCHAR(50) = NULL,
    @sConName NVARCHAR(50) = NULL,
    @sJobOrderId NVARCHAR(50) = NULL,
    @sUsrName NVARCHAR(50) = NULL,
    @bJobClosed BIT = NULL,
    @bJobCanceled BIT = NULL,
    @bCount BIT = 0,
    @bGTCostCenter BIT = 0 
AS
BEGIN
    SET NOCOUNT ON

    IF @bCount = 0
    BEGIN
        IF @bGTCostCenter = 0
        BEGIN
            SELECT CASE WHEN sfVchBelongsTo IS NULL THEN sVchSerial ELSE sfVchBelongsTo END, Jobs.*,
                nkVch, sVchConName, sVchConAddress, sVchConCity, sVchConTel, sVchConZip, sVchDest, sVchCourier,
                sVchSerial, nVchPieces, cVchWeight, sVchDesc, sVchAdditional, cVchTotal, sVchStatus,
                sVchSubCode, sfVchBelongsTo, cVchInsAmount, nfVchJob, sUsrName
            FROM Jobs
            INNER JOIN Users ON nfJobUser = nkUsr
            INNER JOIN Vouchers ON nfVchJob = nkJob
            WHERE nfVchLinkedTo IS NULL AND
            sUsrClientCode = @sUsrClientCode AND
            (@dFr IS NULL OR dJob >= @dFr) AND
            (@dTo IS NULL OR dJob <= @dTo) AND
            (@sVchSerial IS NULL OR sVchSerial LIKE (@sVchSerial + '%')) AND
            (@sConName IS NULL OR sVchConName LIKE (@sConName + '%')) AND
            (@sJobOrderId IS NULL OR sJobOrderId LIKE (@sJobOrderId + '%')) AND
            (@sUsrName IS NULL OR sUsrName = @sUsrName) AND
            (@bJobClosed IS NULL OR bJobClosed = @bJobClosed) AND
            (@bJobCanceled IS NULL OR bJobCanceled = @bJobCanceled)
            ORDER BY 1, nkJob
        END
        ELSE
        BEGIN
            SELECT CASE WHEN sfVchBelongsTo IS NULL THEN sVchSerial ELSE sfVchBelongsTo END, Jobs.*,
                nkVch, sVchConName, sVchConAddress, sVchConCity, sVchConTel, sVchConZip, sVchDest, sVchCourier,
                sVchSerial, nVchPieces, cVchWeight, sVchDesc, sVchAdditional, cVchTotal, sVchStatus,
                COALESCE(κωδικος, '') AS sVchSubCode, sfVchBelongsTo, cVchInsAmount, nfVchJob, sUsrName
            FROM Jobs
            INNER JOIN Users ON nfJobUser = nkUsr
            INNER JOIN Vouchers ON nfVchJob = nkJob
            LEFT JOIN _ΠΕΛΑΤΕΣ_ΚΕΝΤΡΑ_ΧΡΕΩΣΗΣ ON sVchSubCode = Περιγραφη AND Πελατης = sUsrClientCode
            WHERE nfVchLinkedTo IS NULL AND
            sUsrClientCode = @sUsrClientCode AND
            (@dFr IS NULL OR dJob >= @dFr) AND
            (@dTo IS NULL OR dJob <= @dTo) AND
            (@sVchSerial IS NULL OR sVchSerial LIKE (@sVchSerial + '%')) AND
            (@sConName IS NULL OR sVchConName LIKE (@sConName + '%')) AND
            (@sJobOrderId IS NULL OR sJobOrderId LIKE (@sJobOrderId + '%')) AND
            (@sUsrName IS NULL OR sUsrName = @sUsrName) AND
            (@bJobClosed IS NULL OR bJobClosed = @bJobClosed) AND
            (@bJobCanceled IS NULL OR bJobCanceled = @bJobCanceled)
            ORDER BY 1, nkJob
        END
    END
    ELSE
    BEGIN
        SELECT COUNT(*) FROM Jobs
        INNER JOIN Users ON nfJobUser = nkUsr
        INNER JOIN Vouchers ON nfVchJob = nkJob
        WHERE nfVchLinkedTo IS NULL AND
        sUsrClientCode = @sUsrClientCode AND
        (@dFr IS NULL OR dJob >= @dFr) AND
        (@dTo IS NULL OR dJob <= @dTo) AND
        (@sVchSerial IS NULL OR sVchSerial LIKE (@sVchSerial + '%')) AND
        (@sConName IS NULL OR sVchConName LIKE (@sConName + '%')) AND
        (@sJobOrderId IS NULL OR sJobOrderId LIKE (@sJobOrderId + '%')) AND
        (@sUsrName IS NULL OR sUsrName = @sUsrName) AND
        (@bJobClosed IS NULL OR bJobClosed = @bJobClosed) AND
        (@bJobCanceled IS NULL OR bJobCanceled = @bJobCanceled)
    END
END

我发现了类似的问题,但没有回答: Asp.Net Gives a Timeout Error While Running a Stored Procedure

以下是代码:

SqlCommand oCommand = new SqlCommand("GetVoucherJobs");
SqlConnection oConnection;
Result eResult = OpenConnection(out oConnection);
if(eResult != Result.Ok) return new GetJobsResult { eResult = eResult };

oCommand.CommandType = CommandType.StoredProcedure;
oCommand.Connection = oConnection;
oCommand.Parameters.AddWithValue("@sUsrClientCode", oParams.ClientCode);
oCommand.Parameters.AddWithValue("@dFr", oParams.DateFrom == Utils.dNull ? Utils.dNull : oParams.DateFrom);
oCommand.Parameters.AddWithValue("@dTo", oParams.DateTo == Utils.dNull ? DateTime.MaxValue : oParams.DateTo);
oCommand.Parameters.Add("@sVchSerial", SqlDbType.NVarChar);
if(oParams.VoucherNumber != "") oCommand.Parameters["@sVchSerial"].Value = oParams.VoucherNumber;
oCommand.Parameters.Add("@sConName", SqlDbType.NVarChar);
if(oParams.ConsigneeName != "") oCommand.Parameters["@sConName"].Value = oParams.ConsigneeName;
oCommand.Parameters.Add("@sUsrName", SqlDbType.NVarChar);
if(oParams.UserName != "") oCommand.Parameters["@sUsrName"].Value = oParams.UserName;
oCommand.Parameters.Add("@sJobOrderId", SqlDbType.NVarChar);
if(oParams.OrderId != "") oCommand.Parameters["@sJobOrderId"].Value = oParams.OrderId;
oCommand.Parameters.Add("@bJobClosed", SqlDbType.Bit);
oCommand.Parameters.Add("@bJobCanceled", SqlDbType.Bit);
if(oParams.State != VoucherState.All) {
    oCommand.Parameters["@bJobClosed"].Value = oParams.State == VoucherState.Open ? 0 : 1;
    if(oParams.State == VoucherState.ClosedActive || oParams.State == VoucherState.ClosedCanceled) {
        oCommand.Parameters["@bJobCanceled"].Value = oParams.State == VoucherState.ClosedActive ? 0 : 1;
    }
}
oCommand.Parameters.AddWithValue("@bGTCostCenter", oParams.UseGTCostCenter);
oCommand.Parameters.AddWithValue("bCount", 0);

SqlDataReader oReader = oCommand.ExecuteReader();

1 个答案:

答案 0 :(得分:1)

如果您从Management Studio获得不同的结果,可能是您的某些连接设置不同 - 例如ANSI_NULLS,ANSI_PADDING。

尝试在SP中明确设置它们 - 这可能有助于您在Management Studio和.NET中获得相同的行为,这将有助于您的诊断。