public static void Query(string sql, string name, out string realName)
{
string connectionString = "data source=(local);uid=sa;pwd=1;initial catalog=ypth";
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sql;
SqlParameter[] para =
{
new SqlParameter("@name",SqlDbType.VarChar,100),
new SqlParameter("@realName",SqlDbType.VarChar,100)//一定要带上大小,就是第三个参数
};
para[0].Value = name;
para[1].Direction = ParameterDirection.Input;
para[1].Direction = ParameterDirection.Output;
cmd.Parameters.AddRange(para);
SqlDataReader sdr = cmd.ExecuteReader();//输出字符串用ExecueReader
realName = cmd.Parameters[1].Value.ToString();
Console.WriteLine(realName);
}
}
}
调用这个方法
static void Main(string[] args)
{
string realName = "r";
Query("pro_GetName", "jack", out realName);
}