有人知道如何从.net c#读取输出变量吗?
示例:
如果我有以下存储的proc,它将返回输出变量(@customer_id, @customer_name, @customer_address, @customer_age)而不是select变量,我如何读取具有以下内容的输出变量?
mySqlCommand.CommandText = "EXEC app_customers @name=" + sName.Text;
mySqlConnection.Open();
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
while (mySqlDataReader.Read())
{
}发布于 2011-09-15 12:07:58
如果结果是单个值(或者如果您只对第一列中的第一个值感兴趣),请使用ExecuteScalar方法。
它返回一个对象,只需将其转换为预期的类型。
int id = (int)mySqlCommand.ExecuteScalar();注意:调用过程的方式不是正常的方式。将命令设置为引用存储过程,然后向command.Parameters集合添加适当的参数。使用"exec ..."调用该过程不是最佳实践,甚至可能使您容易受到攻击。如果您需要有关执行此类调用的更多信息,请访问start here.
编辑:
如果它确实是您需要捕获的输出参数(我相信我误解了您的问题),那么上面的段落更适用。考虑这种方法:
mySqlCommand.CommandText = "app_customers";
mySqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
mySqlCommand.Parameters.AddWithValue("@name", theValue);
var customerIdParam = mySqlCommand.Parameters.Add("@customer_id", System.Data.SqlDbType.Int);
customerIdParam.Direction = System.Data.ParameterDirection.Output;
// add more parameters, setting direction as appropriate
mySqlCommand.ExecuteNonQuery();
int customerId = (int)customerIdParam.Value;
// read additional outputshttps://stackoverflow.com/questions/7425721
复制相似问题