如何使用C#使用输出调用存储过程

时间:2017-01-23 14:46:38

标签: c# sql-server stored-procedures

我有这个提供输出值的SQL Server存储过程。请注意,输出值是添加新记录时创建的标识种子。

    @EventNumber varchar(12),
    @inc_date_occurred varchar(10),
    @inc_time_occurred int,
    @inc_location varchar (200),
    @inc_location_exempt bit,
    @inc_case_number varchar(9),
    @Zone varchar(50),
    @inc_school_name varchar(50),
    @inc_special_detail_name varchar(50),
    @Inc_ID int output
AS
BEGIN
    IF @EventNumber IS NOT NULL
       --Start Building a Record that comes in with an Event Number
       INSERT INTO Tbl_Incident(inc_event_number, inc_is_uof, inc_is_comp_via_sup,
                                inc_is_comp_via_psd, inc_date_recvd, inc_date_occurred_start,
                                inc_time_occurred_start, inc_location, inc_location_exempt,
                                inc_case_number, inc_status, inc_comp_complaint_to_psd,
                                inc_synopsis, inc_zone, inc_school_name,
                                inc_special_detail_name, inc_arrest_made, 
                                inc_date_entered, inc_entered_by)

      VALUES (@EventNumber, '1', --inc_is_uof,
              '0', --inc_is_comp_via_sup
              '0', --inc_is_comp_via_sup
              GETDATE(),--inc_date-received,
              CAST(@inc_date_occurred AS DATE),--inc_date_occurred
              --start time conversion
              right(convert(varchar(20), cast(stuff(right('0000' + convert(varchar(4),@inc_time_occurred),4),3,0,':') as datetime),100),7),
              -- end time converted from Military to standard
              @inc_location, @inc_location_exempt, @inc_case_number,
              'OPEN',--@inc_status,
              '0', --
              'This is a for REVIEW ONLY',--synopsis,
              @Zone, @inc_school_name, @inc_special_detail_name,
              '0', --@inc_arrest_made
              GETDATE(), --inc_date_entered
              'SharePoint Transfer'--@inc_entered_by
              )

    SET @Inc_ID = @@IDENTITY

我正在使用这个C#代码用必​​要的参数调用这个过程,并希望得到输出整数。

using (SqlCommand _Incident = new SqlCommand("ocso_InsertIncident", _con))
{
    _Incident.CommandType = CommandType.StoredProcedure;

    // set up parameters
    _Incident.Parameters.AddWithValue("@EventNumber", EventNumber);
    _Incident.Parameters.AddWithValue(@"inc_Date_Occurred", DateTime.Parse("01-10-2017"));
    _Incident.Parameters.AddWithValue("@inc_Time_Occurred", "1535");
    _Incident.Parameters.AddWithValue("@inc_Location", "Web Service Entry");
    _Incident.Parameters.AddWithValue("@inc_location_exempt", 0);
    _Incident.Parameters.AddWithValue("@inc_Case_Number", "2107-123456");
    _Incident.Parameters.AddWithValue("@Zone", "34");
    _Incident.Parameters.AddWithValue("@inc_school_name", DBNull.Value);
    _Incident.Parameters.AddWithValue("@inc_special_detail_name", DBNull.Value);

    // Output value
    SqlParameter inc_ID = new SqlParameter("@ID", DbType.Int16);
    inc_ID.Direction = System.Data.ParameterDirection.Output;
    _Incident.Parameters.Add(inc_ID);

    _Incident.ExecuteNonQuery();

    string inc_id = _cmd.Parameters["@ID"].Value.ToString();
}

代码构建但是当我运行它时,我在调用存储过程的执行时收到此错误消息

{"程序或功能' ocso_InsertIncident'需要参数' @ Inc_ID',这是未提供的。"}

所以我尝试通过在代码中包含它来为@INC_ID Paramter传递一个空值

_Incident.Parameters.AddWithValue("@INC_ID", DBNull.Value);

但是我接到错误消息{"过程或函数ocso_InsertIncident指定了太多参数。"}

我似乎被卡住了,请有人帮帮忙

由于

3 个答案:

答案 0 :(得分:1)

您提供了一个名为“ID”的参数,该过程不需要该参数:

SqlParameter inc_ID = new SqlParameter("@ID", DbType.Int16);
inc_ID.Direction = System.Data.ParameterDirection.Output;
_Incident.Parameters.Add(inc_ID);

尝试将此参数重命名为“@Inc_ID”:

SqlParameter inc_ID = new SqlParameter("@Inc_ID", DbType.Int16);
inc_ID.Direction = System.Data.ParameterDirection.Output;
_Incident.Parameters.Add(inc_ID);

答案 1 :(得分:1)

删除此代码

_Incident.Parameters.AddWithValue("@INC_ID", DBNull.Value);

并像这样修改您的代码

SqlParameter inc_ID = new SqlParameter("@INC_ID", DbType.Int16);
                    inc_ID.Direction = System.Data.ParameterDirection.Output;
                    _Incident.Parameters.Add(inc_ID);
                    _Incident.ExecuteNonQuery();

                    string inc_id = _cmd.Parameters["@INC_ID"].Value.ToString();

答案 2 :(得分:1)

您不需要为参数提供值,并应指示它是输出参数。而不是:

_Incident.Parameters.AddWithValue("@INC_ID", DBNull.Value);

使用:

SqlParameter outputValue = new SqlParameter("@INC_ID", SqlDbType.Int);
outputValue.Direction = ParameterDirection.Output;
cmd.Parameters.Add(outputValue);