1

我有这样的参数;

    comm.CommandText = "usp_ProjectsCreateNew";

    SqlParameter param = comm.CreateParameter();
    param.ParameterName = "@ProjectId";
    param.Direction = ParameterDirection.Output;
    param.DbType = DbType.Int32;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectHeading";
    param.Value = heading;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectLongDescription";
    param.Value = longSummary;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectUrl";
    param.Value = url;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@PromoFront";
    param.Value = promoFront;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectThumbnail";
    param.Value = thumbnailFileName;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectImage2";
    param.Value = imageFileName1;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectImage3";
    param.Value = imageFileName2;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectImage4";
    param.Value = imageFileName3;
    comm.Parameters.Add(param);

    param = comm.CreateParameter();
    param.ParameterName = "@ProjectImage5";
    param.Value = imageFileName4;
    comm.Parameters.Add(param);

    comm.Transaction = tran;
    int result = comm.ExecuteNonQuery();

我的存储过程代码是这样的;

CREATE PROC [dbo].[usp_ProjectsCreateNew]
(
    @ProjectId INT output,
    @ProjectHeading varchar(100),
    @ProjectLongDescription varchar(max),
    @ProjectUrl varchar(100),
    @PromoFront bit,
    @ProjectThumbnail varchar(50),
    @ProjectImage2 varchar(50),
    @ProjectImage3 varchar(50),
    @ProjectImage4 varchar(50),
    @ProjectImage5 varchar(50)
)
AS
BEGIN TRY
    BEGIN TRAN
        INSERT INTO [Projects]([ProjectHeading], [ProjectLongSummary], [ProjectUrl],
            [PromoFront], [ProjectThumbnail], [ProjectImage2], [ProjectImage3], [ProjectImage4], [ProjectImage5])
        VALUES(@ProjectHeading, @ProjectLongDescription, @ProjectUrl, 
            @PromoFront, @ProjectThumbnail, @ProjectImage2, @ProjectImage3, @ProjectImage4, @ProjectImage5);

        SELECT @ProjectId = @@IDENTITY;
    COMMIT TRAN
END Try
BEGIN CATCH
    PRINT Error_Message();
    ROLLBACK TRAN
END CATCH

但是我收到了这个错误;

消息:过程或函数“usp_ProjectsCreateNew”需要参数“@ProjectId”,但未提供该参数。

来源:.Net SqlClient 数据提供者

方法:Void OnError(System.Data.SqlClient.SqlException, Boolean, System.Action`1[System.Action])

4

1 回答 1

2

首先,你没有告诉SqlCommand它它是一个存储过程!

添加这一行:

comm.CommandType = CommandType.StoredProcedure;

在设置comm'.CommandText属性之后。

然后:你的一些参数不清楚 - 我建议你使用这样的东西来创建和添加你的参数到SqlCommand对象:

cmd.Parameters.Add("@PromoFront", SqlDbType.Bit).Value = 1;

我总是会SqlDbType为每个参数明确定义一个,以使其绝对清楚它是什么。不这样做可能会导致不需要的结果。

于 2012-07-28T10:33:44.357 回答