如何运行从C#输出参数的存储过程?
我有一个带有输出参数的存储过程。如何使用C代码读取此值?我假设您使用ADO.NET?如果是,SqlParameter类具有属性“Direction”。将direction设置为output,并在执行查询后从该参数中读取值 大概是这样的:如何运行从C#输出参数的存储过程?,c#,C#,我有一个带有输出参数的存储过程。如何使用C代码读取此值?我假设您使用ADO.NET?如果是,SqlParameter类具有属性“Direction”。将direction设置为output,并在执行查询后从该参数中读取值 大概是这样的: using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn)) { cmd.CommandType = CommandType.StoredProcedure; SqlParame
using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parm = new SqlParameter("@pkid", SqlDbType.Int);
parm.Value = 1;
parm.Direction = ParameterDirection.Input;
cmd.Parameters.Add(parm);
SqlParameter parm2 = new SqlParameter("@ProductName", SqlDbType.VarChar);
parm2.Size = 50;
parm2.Direction = ParameterDirection.Output; // This is important!
cmd.Parameters.Add(parm2);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
请发布你到目前为止所写的代码。我会对此进行投票,因为这是一个有用的问题,但由于缺少“我尝试过这个”,我不能这样做。我还有一个问题,我需要确定输出参数是十进制(8,2)如何!!!我不确定我是否理解这个问题。如果要在输出变量中返回小数,则应将SqlDbType设置为decimal。如果您实际上返回的是一个十进制数,您可以这样强制转换:(decimal)cmd.Parameters[@“MyDecimal”].ValueI强烈建议将
SqlConnection
和SqlCommand
放入中,最好使用(…..){…}
块作为最佳实践调用stroc embedded或执行嵌套stroc时遇到问题