存储过程在DB中有效,但在C#代码中无效

时间:2014-12-31 12:04:19

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

我有一个存储过程,它有3个输入参数。 Id和2个VarChar条件。当我执行存储过程的干运行时,它工作正常,但是当我从C#代码调用相同的存储过程时,它会失败。我传递了与存储过程的干运行中使用的完全相同的参数,但存储过程一直挂起。

有没有理由这样做?

我正在使用SQL Server 2008 R2快速版。

这是存储过程的干运行:

EXEC    @return_value = [dbo].[GetAttributes]
        @pi_PId = 95102,
        @pi_returnOnly1stRow = 0,
        @pi_returnExtAttr = 1

SELECT  'Return Value' = @return_value

这是来自C#代码的调用:

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("GetAttributes", conn);
da.SelectCommand.Parameters.AddWithValue("@pi_PId", 95102); 
da.SelectCommand.Parameters.AddWithValue("@pi_returnOnly1stRow", 0);
da.SelectCommand.Parameters.AddWithValue("@pi_returnExtAttr", 1);
da.SelectCommand.CommandType = CommandType.StoredProcedure;

DataSet ds = new DataSet();
da.Fill(ds, "result_name");
DataTable dt = ds.Tables["result_name"];
conn.Close();

编辑:问题返回....

我以为我已经通过杀死引起这个问题的过程解决了这个问题,但不幸的是它又回来了。我说我对这个问题有了更好的了解,我可以看到问题,但不知道是什么导致这一点。

问题是一样的。如果直接在SQL服务器上运行,则存储过程运行正常但在从C#程序调用时无法执行....

我跑了Sp_lock。我使用的存储过程有Spid:' 59'锁的显示如下。我不知道为什么这些表只有在从c#调用存储过程时才被锁定,而不是在SP的干运行期间被锁定。

59  2   0           0   DB  [ENCRYPTION_SCAN]                   S   GRANT
59  5   1802489500  0   PAG 1:169937                            S   GRANT
59  5   1914489899  0   TAB                                     IS  GRANT
59  5   1898489842  0   TAB                                     IS  GRANT
59  5   1177771253  0   TAB                                     IS  GRANT
59  5   1786489443  0   TAB                                     IS  GRANT
59  5   1802489500  0   TAB                                     IS  GRANT
59  5   1882489785  0   TAB                                     IS  GRANT
59  5   0           0   DB                                      S   GRANT

最新编辑:我还通过在SP中涉及的每个表中使用NOLOCK来介绍我的Sp

我也在这里粘贴存储过程供您参考(添加了最新的NOLOCK)....

    @pi_PId                 INT
,   @pi_returnOnly1stRow    BIT
,   @pi_returnExtAttr       BIT
AS

BEGIN 
    IF(@pi_returnOnly1stRow=1)
        BEGIN
            SELECT TOP 1 NULL section_name,header_mvoc.text Attr_Name, body_mvoc.Text Attr_Value,cds_mspecee.DisplayOrder
            FROM   cds_mspecee WITH (NOLOCK)
            JOIN   cds_mvocee header_mvoc WITH (NOLOCK)ON (cds_mspecee.hdrid = header_mvoc.id)
            JOIN   cds_mvocee body_mvoc WITH (NOLOCK) ON (cds_mspecee.bodyid = body_mvoc.id)
            JOIN   cds_prod WITH (NOLOCK)ON (cds_Prod.prodid = cds_mspecee.prodid)
            JOIN   ProductVariant revpro WITH (NOLOCK) On (revpro.ManufacturerSKU=cds_prod.mfpn)
            AND    revpro.ProductID = @pi_PId
            UNION
            SELECT section_evoc.Text section_name, header_evoc.text Attr_Name, body_evoc.Text Attr_Value, cds_Especee.DisplayOrder
            FROM   cds_Especee WITH (NOLOCK) 
            JOIN   cds_Evocee section_evoc WITH (NOLOCK) ON (cds_Especee.SectID = section_evoc.ID)
            JOIN   cds_Evocee header_evoc WITH (NOLOCK) ON (cds_Especee.hdrid = header_evoc.id)
            JOIN   cds_Evocee body_evoc WITH (NOLOCK) ON (cds_Especee.bodyid = body_evoc.id)
            JOIN   cds_prod WITH (NOLOCK) ON (cds_Prod.prodid = cds_especee.prodid)
            JOIN   ProductVariant revpro WITH (NOLOCK) On (revpro.ManufacturerSKU=cds_prod.mfpn)
            AND    revpro.ProductID = @pi_PId
            AND    @pi_returnExtAttr = 1
            ORDER BY section_name,displayorder ASC
        END
    ELSE
        BEGIN
            SELECT NULL section_name ,header_mvoc.text Attr_Name, body_mvoc.Text Attr_Value,cds_mspecee.DisplayOrder
            FROM   cds_mspecee WITH (NOLOCK) 
            JOIN   cds_mvocee header_mvoc WITH (NOLOCK) ON (cds_mspecee.hdrid = header_mvoc.id)
            JOIN   cds_mvocee body_mvoc WITH (NOLOCK) ON (cds_mspecee.bodyid = body_mvoc.id)
            JOIN   cds_prod WITH (NOLOCK) ON (cds_Prod.prodid = cds_mspecee.prodid)
            JOIN   productVariant revpro WITH (NOLOCK) On (revpro.manufacturerSKU=cds_prod.mfpn)
            AND    revpro.ProductID = @pi_PId
            UNION
            SELECT section_evoc.Text section_name,header_evoc.text Attr_Name, body_evoc.Text Attr_Value,cds_Especee.DisplayOrder
            FROM   cds_Especee WITH (NOLOCK) 
            JOIN   cds_Evocee section_evoc WITH (NOLOCK) ON (cds_Especee.sectid = section_evoc.id)
            JOIN   cds_Evocee header_evoc WITH (NOLOCK) ON (cds_Especee.hdrid = header_evoc.id)
            JOIN   cds_Evocee body_evoc WITH (NOLOCK) ON (cds_Especee.bodyid = body_evoc.id)
            JOIN   cds_prod WITH (NOLOCK) ON (cds_Prod.prodid = cds_especee.prodid)
            JOIN   productVariant revpro WITH (NOLOCK) On (revpro.manufacturerSKU=cds_prod.mfpn)
            AND    revpro.ProductID = @pi_PId
            AND    @pi_returnExtAttr = 1
            ORDER BY section_name,displayorder ASC
        END
END

3 个答案:

答案 0 :(得分:5)

这听起来好像是"参数嗅探" (http://blogs.technet.com/b/mdegre/archive/2012/03/19/what-is-parameter-sniffing.aspx),这意味着可以根据先前执行的过程针对不同的参数值优化为您的过程缓存的执行计划。此缓存计划可能对参数值x有效,但对参数值y无效。我之前遇到过同样的问题,当我在SSMS中运行查询时,它会立即运行,但如果我从我的应用程序运行,它会挂起"。这是因为我在SSMS中运行时的查询文本与来自应用程序的查询文本略有不同,因此它使用不同的缓存执行计划。

解决此问题的一种解决方法是在proc中创建一个本地范围的变量,它们充当过程参数的副本。

我还建议:

  1. 如果您没有从UNION的每一侧返回重复的行,请将UNION替换为UNION ALL以删除SQL Server检查重复值所不需要的开销在行中。 What is the difference between UNION and UNION ALL?
  2. 避免使用WITH(NOLOCK)提示,因为这些可能会导致数据一致性问题。如果您完全理解副作用,则只使用此提示,并且您的应用程序可以使用脏读取并且可能多次返回相同的行(When should you use "with (nolock)")。
  3. 以下是结合上述反馈的示例:

    ALTER PROC dbo.usp_YourProcName
        @pi_PId                 INT
    ,   @pi_returnOnly1stRow    BIT
    ,   @pi_returnExtAttr       BIT
    AS
    BEGIN 
        --these local variables are used to address "parameter sniffing" issues which may cause an ineffient plan cache.
        --Use these local variables below instead of the direct parameter values.
        DECLARE @local_pi_PId             INT = @pi_PId
        ,   @local_pi_returnOnly1stRow    BIT = @pi_returnOnly1stRow
        ,   @local_pi_returnExtAttr       BIT = @pi_returnExtAttr
        ;
    
        IF(@local_pi_returnOnly1stRow=1)
            BEGIN
                SELECT TOP 1 NULL section_name,header_mvoc.text Attr_Name, body_mvoc.Text Attr_Value,cds_mspecee.DisplayOrder
                FROM   cds_mspecee 
                JOIN   cds_mvocee header_mvoc ON (cds_mspecee.hdrid = header_mvoc.id)
                JOIN   cds_mvocee body_mvoc  ON (cds_mspecee.bodyid = body_mvoc.id)
                JOIN   cds_prod ON (cds_Prod.prodid = cds_mspecee.prodid)
                JOIN   ProductVariant revpro  On (revpro.ManufacturerSKU=cds_prod.mfpn)
                AND    revpro.ProductID = @local_pi_PId
                UNION ALL
                SELECT section_evoc.Text section_name, header_evoc.text Attr_Name, body_evoc.Text Attr_Value, cds_Especee.DisplayOrder
                FROM   cds_Especee  
                JOIN   cds_Evocee section_evoc  ON (cds_Especee.SectID = section_evoc.ID)
                JOIN   cds_Evocee header_evoc  ON (cds_Especee.hdrid = header_evoc.id)
                JOIN   cds_Evocee body_evoc  ON (cds_Especee.bodyid = body_evoc.id)
                JOIN   cds_prod  ON (cds_Prod.prodid = cds_especee.prodid)
                JOIN   ProductVariant revpro  On (revpro.ManufacturerSKU=cds_prod.mfpn)
                AND    revpro.ProductID = @local_pi_PId
                AND    @local_pi_returnExtAttr = 1
                ORDER BY section_name,displayorder ASC
            END
        ELSE
            BEGIN
                SELECT NULL section_name ,header_mvoc.text Attr_Name, body_mvoc.Text Attr_Value,cds_mspecee.DisplayOrder
                FROM   cds_mspecee  
                JOIN   cds_mvocee header_mvoc  ON (cds_mspecee.hdrid = header_mvoc.id)
                JOIN   cds_mvocee body_mvoc  ON (cds_mspecee.bodyid = body_mvoc.id)
                JOIN   cds_prod  ON (cds_Prod.prodid = cds_mspecee.prodid)
                JOIN   productVariant revpro  On (revpro.manufacturerSKU=cds_prod.mfpn)
                AND    revpro.ProductID = @local_pi_PId
                UNION ALL
                SELECT section_evoc.Text section_name,header_evoc.text Attr_Name, body_evoc.Text Attr_Value,cds_Especee.DisplayOrder
                FROM   cds_Especee  
                JOIN   cds_Evocee section_evoc  ON (cds_Especee.sectid = section_evoc.id)
                JOIN   cds_Evocee header_evoc  ON (cds_Especee.hdrid = header_evoc.id)
                JOIN   cds_Evocee body_evoc  ON (cds_Especee.bodyid = body_evoc.id)
                JOIN   cds_prod  ON (cds_Prod.prodid = cds_especee.prodid)
                JOIN   productVariant revpro  On (revpro.manufacturerSKU=cds_prod.mfpn)
                AND    revpro.ProductID = @local_pi_PId
                AND    @local_pi_returnExtAttr = 1
                ORDER BY section_name,displayorder ASC
            END
    END
    

答案 1 :(得分:1)

启动SQL分析器并执行代码。检查命中即将发送到探查器。从那里复制并在编辑器窗口中运行以检查统计数据。

答案 2 :(得分:0)

使用此

SqlConnection con=new SqlConnection("ConnecttionString"); 
con.Open();
SqlCommand cmd = new SqlCommand("ProcedureName", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("StoredProcedureParameter", Value);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp .Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
 // get the data table field value here 
}
else
{
// Table is Empty 
}