C# 调用存储过程 输出参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace CallSp2 { class CallSp2 { static void Main(string[] args) { //创建数据库连接对象 SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); try { conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_orders_by_employeeid2"; //创建输入参数 SqlParameter inparm = cmd.Parameters.Add("@employeeid", SqlDbType.Int); inparm.Direction = ParameterDirection.Input; inparm.Value = 2; //创建“输出”参数 SqlParameter ouparm = cmd.Parameters.Add("@ordercount", SqlDbType.Int); ouparm.Direction = ParameterDirection.Output;//方向 //create return value parameter SqlParameter retval = cmd.Parameters.Add("return_value", SqlDbType.Int); retval.Direction = ParameterDirection.ReturnValue; SqlDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { Console.WriteLine("{0} {1}", rdr[0].ToString().PadRight(5), rdr[1].ToString()); } rdr.Close(); //显示输出参数 Console.WriteLine("The output parameter value is {0}", cmd.Parameters["@ordercount"].Value); Console.WriteLine("The return_value is {0}", cmd.Parameters["return_value"].Value); } catch (SqlException ex) { Console.WriteLine(ex.ToString()); } finally { conn.Close(); } } } }