我想将using
用于sql command
:
using(SqlCommand command = new SqlCommand("usp_UpdateUser", _sqlConnection, _sqlTransaction))
{
....
}
但在FillCommand(ref command)
我得到例外:
Cannot pass command as ref or out variable because it is using variable.
我的代码是:
SqlCommand command = new SqlCommand("usp_UpdateUser", _sqlConnection,
_sqlTransaction);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int)).Value = Id;
FillCommand(ref command);
try
{
command.ExecuteNonQuery();
}
catch
{
throw;
}
答案 0 :(得分:2)
自
FillCommand
是您编写的函数,并且您将其用作void
函数,您可以对其进行修改以使其返回命令,而不是将其作为ref
参数传递。
使用类似的东西:
using(SqlCommand command = FillCommand("usp_UpdateUser", _sqlConnection, _sqlTransaction, CommandType.StoredProcedure))
{
...
}