没有提供值到输出参数

时间:2014-08-19 09:42:40

标签: c# ado.net sqlparameters output-parameter

我收到错误,因为“程序或功能'Artical_i'需要参数'@ID',这是未提供的。”但我的@ID参数是输出参数。

------------------------------- c#Code -------------- --------------------------

int retVal=0;
SqlParameter outputParam = new SqlParameter();
outputParam.ParameterName = "@ID";
outputParam.SqlDbType = SqlDbType.Int;
outputParam.Direction = ParameterDirection.Output;
outputParam.Value = retVal; /// Added in Edit
SqlParameter[] sParam = 
                    {
                        outputParam,
                        new SqlParameter("@Title",this.Title),
                        new SqlParameter("@BaseUri",this.BaseUri),
                        new SqlParameter("@Description",this.Description),
                        new SqlParameter("@ShortDescription",this.ShortDescription),
                        new SqlParameter("@Copyright",this.Copyright),
                        new SqlParameter("@ItemID",this.ItemID),
                        new SqlParameter("@LastUpdatedTime",this.LastUpdatedTime),
                        new SqlParameter("@PublishDate",this.PublishDate),
                        new SqlParameter("@SourceFeedURL",this.SourceFeedURL)
                    };



SqlConnection connection = new SqlConnection(connectionString)
SqlCommand command = new SqlCommand();
connection.Open();
command.Connection = connection;
command.CommandText = procedureName;
command.CommandType = CommandType.StoredProcedure;

foreach (SqlParameter p in sParam )
{
      command.Parameters.AddWithValue(p.ParameterName, p.Value);
}

command.ExecuteNonQuery(); /// Error Here as "Procedure or function 'Artical_i' expects parameter '@ID', which was not supplied."

retVal = Convert.ToInt32(outputParam.Value); // Value returned as 0 here

--------------------存储过程------------------------- -

IF OBJECT_ID (N'Artical_i', N'P') IS NOT NULL
Begin
    drop procedure Artical_i
End

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create Procedure Artical_i
(
    @Title nvarchar(500),
    @BaseUri nvarchar(2083),
    @Description nvarchar(max),
    @ShortDescription nvarchar(500),
    @Copyright nvarchar(250),
    @ItemID varchar(250),
    @LastUpdatedTime datetimeoffset(7),
    @PublishDate datetimeoffset(7),
    @SourceFeedURL nvarchar(2083),
    @ID int OUTPUT
)

as
Begin

    BEGIN TRY
        BEGIN TRANSACTION

            set nocount on;

            INSERT INTO [dbo].[Artical]
                       ([Title]
                       ,[BaseUri]
                       ,[Description]
                       ,[ShortDescription]
                       ,[Copyright]
                       ,[ItemID]
                       ,[LastUpdatedTime]
                       ,[PublishDate]
                       ,[CreatedDate]
                       ,[SourceFeedURL])
                 VALUES
                       (@Title,
                       @BaseUri,
                       @Description,
                       @ShortDescription,
                       @Copyright,
                       @ItemID,
                       @LastUpdatedTime,
                       @PublishDate,
                       Getdate(),
                       @SourceFeedURL)


            Select @ID =SCOPE_IDENTITY()

        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION

        Select @ID =0


    END CATCH

End
GO

0 个答案:

没有答案