简体   繁体   English

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

[英]How To Call a Stored Procedure with an Output using C#

I have this SQL Server stored procedure that provides an output value. 我有提供输出值的此SQL Server存储过程。 Note the output value is the identity seed that gets created when the new record is added. 请注意,输出值是添加新记录时创建的标识种子。

    @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

I am using this C# code to call this procedure with the necessary parameters and would like to get the output integer back. 我正在使用此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();
}

The code builds but when I run it I am getting this error message when the execution of the stored procedure is called 代码会生成,但是当我运行它时,调用存储过程的执行时会收到此错误消息

{"Procedure or function 'ocso_InsertIncident' expects parameter '@Inc_ID', which was not supplied."} {“过程或函数'ocso_InsertIncident'期望参数'@Inc_ID',但未提供。”}

So I tried passing a null value for the @INC_ID Paramter by including this in the code 所以我尝试通过在代码中包含@INC_ID参数来传递空值

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

but I then get the error message {"Procedure or function ocso_InsertIncident has too many arguments specified."} 但我随后收到错误消息{“过程或函数ocso_InsertIncident所指定的参数过多。”}

I seem to be stuck can someone help out please 我似乎被困住了,有人可以帮忙吗

Thanks 谢谢

You provide a parameter called "ID" that is not needed by the procedure: 您提供了过程不需要的名为“ ID”的参数:

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

Try renaming this parameter to "@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);

Remove this code 删除此代码

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

and modify your codes like this 并像这样修改您的代码

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();

You don't need to provide a value for the parameter, and should indicate it is an output parameter. 您无需为参数提供值,而应指出它是输出参数。 Instead of: 代替:

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

Use: 采用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM