首页 > 编程语言 >C# 调用存储过程

C# 调用存储过程

时间:2023-10-02 13:33:05浏览次数:49  
标签:存储 调用 Parameters C# cmd parReturn parOutput new conn

转载: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;
}
}
}

标签:存储,调用,Parameters,C#,cmd,parReturn,parOutput,new,conn
From: https://www.cnblogs.com/KevinSteven/p/17733255.html

相关文章