Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未向输出参数提供任何值_C#_Ado.net_Sqlparameter_Output Parameter - Fatal编程技术网

C# 未向输出参数提供任何值

C# 未向输出参数提供任何值,c#,ado.net,sqlparameter,output-parameter,C#,Ado.net,Sqlparameter,Output Parameter,由于过程或函数“Artical_I”需要参数“@ID”,而未提供该参数,因此我收到错误。。但是我的@ID参数是输出参数 ----------------c代码-------------------- int retVal=0; SqlParameter outputParam = new SqlParameter(); outputParam.ParameterName = "@ID"; outputParam.SqlDbType = SqlDbType.Int; outputParam.Dir

由于过程或函数“Artical_I”需要参数“@ID”,而未提供该参数,因此我收到错误。。但是我的@ID参数是输出参数

----------------c代码--------------------

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
----------存储过程--------------

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

在新建后添加outputParamSqlParameter@SourceFeedURL,this.SourceFeedURL在LastTryed,错误仍然存在。添加参数的foreach循环未设置.Direction属性。您可以重新创建新的SqlParameter,而不是使用尚未创建的SqlParameter。你应该使用.Addp,而不是.AddWithValue…嗯,听起来不错,让我试试看!好朋友!请将此添加为ans,以便我可以标记为有用。