How to use OUTPUT parameter in Stored Procedure

后端 未结 5 493
青春惊慌失措
青春惊慌失措 2020-12-18 05:21

I am new to writing Stored Procedure. So I wrote one with output parameters and want to access the output value, hot to do it.

My Stored Procedure:

A         


        
相关标签:
5条回答
  • 2020-12-18 06:02

    You need to close the connection before you can use the output parameters. Something like this

    con.Close();
    MessageBox.Show(cmd.Parameters["@code"].Value.ToString());
    
    0 讨论(0)
  • 2020-12-18 06:09

    There are a several things you need to address to get it working

    1. The name is wrong its not @ouput its @code
    2. You need to set the parameter direction to Output.
    3. Don't use AddWithValue since its not supposed to have a value just you Add.
    4. Use ExecuteNonQuery if you're not returning rows

    Try

    SqlParameter output = new SqlParameter("@code", SqlDbType.Int);
    output.Direction = ParameterDirection.Output;
    cmd.Parameters.Add(output);
    cmd.ExecuteNonQuery();
    MessageBox.Show(output.Value.ToString());
    
    0 讨论(0)
  • 2020-12-18 06:12

    The SQL in your SP is wrong. You probably want

    Select @code = RecItemCode from Receipt where RecTransaction = @id
    

    In your statement, you are not setting @code, you are trying to use it for the value of RecItemCode. This would explain your NullReferenceException when you try to use the output parameter, because a value is never assigned to it and you're getting a default null.

    The other issue is that your SQL statement if rewritten as

    Select @code = RecItemCode, RecUsername from Receipt where RecTransaction = @id
    

    It is mixing variable assignment and data retrieval. This highlights a couple of points. If you need the data that is driving @code in addition to other parts of the data, forget the output parameter and just select the data.

    Select RecItemCode, RecUsername from Receipt where RecTransaction = @id
    

    If you just need the code, use the first SQL statement I showed you. On the offhand chance you actually need the output and the data, use two different statements

    Select @code = RecItemCode from Receipt where RecTransaction = @id
    Select RecItemCode, RecUsername from Receipt where RecTransaction = @id
    

    This should assign your value to the output parameter as well as return two columns of data in a row. However, this strikes me as terribly redundant.

    If you write your SP as I have shown at the very top, simply invoke cmd.ExecuteNonQuery(); and then read the output parameter value.


    Another issue with your SP and code. In your SP, you have declared @code as varchar. In your code, you specify the parameter type as Int. Either change your SP or your code to make the types consistent.


    Also note: If all you are doing is returning a single value, there's another way to do it that does not involve output parameters at all. You could write

     Select RecItemCode from Receipt where RecTransaction = @id
    

    And then use object obj = cmd.ExecuteScalar(); to get the result, no need for an output parameter in the SP or in your code.

    0 讨论(0)
  • 2020-12-18 06:13
    SqlCommand yourCommand = new SqlCommand();
    yourCommand.Connection = yourSqlConn;
    yourCommand.Parameters.Add("@yourParam");
    yourCommand.Parameters["@yourParam"].Direction = ParameterDirection.Output;
    
    // execute your query successfully
    
    int yourResult = yourCommand.Parameters["@yourParam"].Value;
    
    0 讨论(0)
  • 2020-12-18 06:17

    You need to define the output parameter as an output parameter in the code with the ParameterDirection.Output enumeration. There are numerous examples of this out there, but here's one on MSDN.

    0 讨论(0)
提交回复
热议问题