Getting return value from stored procedure in ADO.NET

前端 未结 5 1496
感动是毒
感动是毒 2020-12-09 03:43

I know that there is another question with nearly the same title, but it doesn\'t answer my question. I have a stored procedure, which returns the unique identifier after in

5条回答
  •  有刺的猬
    2020-12-09 04:24

    Just tried on my box and this works for me:

    In SQL Server:

    DROP PROCEDURE TestProc;
    GO
    CREATE PROCEDURE TestProc
    AS
       RETURN 123;
    GO
    

    In C#

            string cnStr = "Server=.;Database=Sandbox;Integrated Security=sspi;";
            using (SqlConnection cn = new SqlConnection(cnStr)) {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("TestProc", cn)) {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter returnValue = new SqlParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    cmd.Parameters.Add(returnValue);
    
                    cmd.ExecuteNonQuery();
                    Assert.AreEqual(123, (int)returnValue.Value);
                }
            }
    

提交回复
热议问题