过程或函数“”需要参数'',这是未提供的

时间:2010-09-09 13:06:27

标签: c# asp.net sql

我遇到了数据库问题。必须使用3或4个参数调用存储的Proc。如果没有给出ImageID,那么它必须 进入If循环并执行。如果给出了ImageID,则执行存储过程的else部分。但我不知道为什么会这样 过程或函数“”需要参数'@ImageID',这是未提供的。“

先谢谢

if (!hasImage)
{
    parameters = new SqlParameter[]
    {
        new SqlParameter("@LocationID", LocationID),
        new SqlParameter("@PrimaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
        new SqlParameter("@SecondaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[1]))
    };

    SqlHelper.ExecuteNonQuery(
        DbConnString, 
        System.Data.CommandType.StoredProcedure,
        "TempUpdateMerchantCategories_Insert", 
        parameters);
}
else
{
    parameters = new SqlParameter[]
    {
        new SqlParameter("@LocationID", LocationID),
        new SqlParameter("@PrimaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
        new SqlParameter("@SecondaryID", 
            Convert.ToInt32(CategoryList[i].ToString().Split(',')[1])),
        new SqlParameter("@ImageID", 
            Convert.ToInt64(ImageData[j].ToString().Split(',')[0]))
    };
    SqlHelper.ExecuteNonQuery(
        DbConnString, 
        System.Data.CommandType.StoredProcedure,
        "TempUpdateMerchantCategories_Insert", 
        parameters);       
}

存储过程是这样的。

CREATE PROCEDURE [dbo].[TempUpdateMerchantCategories_Insert]
(
@LocationID BIGINT,
@PrimaryID INT,
@SecondaryID INT,
@ImageID BIGINT)

AS
BEGIN

if (@ImageID is null)

BEGIN
SET NOCOUNT ON;

INSERT INTO TempMerchant_Location_Category(

LocationID,
PrimaryID,
SecondaryID)

VALUES (
@LocationID,
@PrimaryID,
@SecondaryID)

END

ELSE

BEGIN
SET NOCOUNT ON;

INSERT INTO TempMerchant_Location_Category( 
LocationID,
PrimaryID,
SecondaryID,
ImageID)  

VALUES(
@LocationID,
@PrimaryID,
@SecondaryID,
@ImageID)

END
END

3 个答案:

答案 0 :(得分:10)

在这种情况下像这样创建你的proc

    CREATE PROCEDURE [dbo].[TempUpdateMerchantCategories_Insert] 
( @LocationID BIGINT, 
@PrimaryID INT, 
@SecondaryID INT, 
@ImageID BIGINT = null)

这将使ImageID成为可选参数

答案 1 :(得分:0)

没有默认值,它需要一些值

CREATE PROCEDURE [dbo].[TempUpdateMerchantCategories_Insert] ( @LocationID BIGINT, @PrimaryID INT, @SecondaryID INT, @ImageID BIGINT = null)

答案 2 :(得分:0)

您必须通过代码传递@ImageID参数,否则您可以在存储过程中为@ImageID设置默认值。

喜欢@ImageID BIGINT = null