存储过程返回1个输出参数问题,这两种方法有区别吗,为什么第一种不行呢
该方法返回存储过程的个输出参数:
SqlParameter[] paramter = new SqlParameter[]
{
new SqlParameter("@id",SqlDbType.VarChar),
new SqlParameter("@i",SqlDbType.Int)
};
paramter[0].Value = id;
paramter[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery((DBConfig.getConfig(), "dbo_turnprivate", paramter);
string inResult = paramter[1].Value.ToString();
return inResult;
这样写会出错,因为paramter[1].Value为空,不能ToString.这里 using Microsoft.ApplicationBlocks.Data;
好象是微软封装好的方法,一个DLL文件.
如果这么写就可以:
SqlConnection conn = new SqlConnection(DBConfig.getConfig());
conn.Open();
SqlCommand cmd = new SqlCommand("turnprivate", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar);
cmd.Parameters["@id"].Value = id;
cmd.Parameters.Add("@i", SqlDbType.Int);
cmd.Parameters["@i"].Direction = ParameterDirection.Output;
cmd.ExecuteReader();
// 获取执行存储过程后的输出参数
string inResult = cmd.Parameters["@i"].Value.ToString();
return inResult;
不可能是封装好的方法不能解决这个问题呀,肯定是我代码哪不对,望高手帮帮忙,谢谢了!