0
using (var con = connection)
            {
                con.Open();
                using (var cmd = new SqlCommand("usp_Students", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter sp = cmd.Parameters.AddWithValue("@StudentIDs", studentId);
                    sp.SqlDbType = SqlDbType.Structured;
                    cmd.ExecuteNonQuery();
                }
            }

我正在向存储过程输入一个值,但在这种情况下,我也想取回一个值。我怎样才能在上面的代码中合并输出?

CREATE PROCEDURE usp_Students
      @StudentId Students READONLY,
      @NumberOfStudents INT OUTPUT,
4

1 回答 1

0

试试这个方法

using (var con = connection)
{
    con.Open();
    using (var cmd = new SqlCommand("usp_Students", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter sp = cmd.Parameters.AddWithValue("@StudentIDs", studentId);
        sp.SqlDbType = SqlDbType.Structured;

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

        cmd.ExecuteNonQuery();

        // Getting output value
        int NumberOfStudent = Convert.ToInt32(cmd.Parameters["@NumberOfStudents"].Value);

    }
}

谢谢

马诺伊

于 2013-06-26T06:59:37.427 回答