C# 调用存储过程
转载:https://blog.csdn.net/m372838677/article/details/121360607
string connString = "Server=MATEBOOK16S\\SQLEXPRESS;DataBase=MISDB;Uid=sa;Pwd=123456";
void StoredProcedure()
{
using(SqlConnection conn = new SqlConnection(connString))
{
try
{
SqlCommand cmd = new SqlCommand("GetNameById",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studentId","88888888"); //给输入参数赋值
SqlParameter parOutput = cmd.Parameters.Add("@studentname",SqlDbType.NVarChar,50); //定义输出参数
parOutput.Direction = ParameterDirection.Output; //参数类型为Output
SqlParameter parReturn = new SqlParameter("@return",SqlDbType.Int);
parReturn.Direction = ParameterDirection.ReturnValue; //参数类型为ReturnValue
cmd.Parameters.Add(parReturn);
conn.Open();
cmd.ExecuteNonQuery();
Console.WriteLine(parOutput.Value.ToString()); //显示输出参数的值
Console.WriteLine(parReturn.Value.ToString()); //显示返回值
}
catch (Exception)
{
throw;
}
}
}