How to use OUTPUT parameter in Stored Procedure

后端 未结 5 494
青春惊慌失措
青春惊慌失措 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条回答
  •  Happy的楠姐
    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());
    

提交回复
热议问题