0

我想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;
}
4

1 回答 1

2

自从

FillCommand 

是您编写的函数并且您将其用作void函数,您可以修改它以使其返回命令而不是将其作为ref参数传递。

使用这样的东西:

using(SqlCommand command = FillCommand("usp_UpdateUser", _sqlConnection, _sqlTransaction, CommandType.StoredProcedure))
{
   ...
}
于 2013-01-08T09:40:40.657 回答