如何在fillCommand中使用?

时间:2013-01-08 09:29:24

标签: c# ado.net sqlcommand

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

1 个答案:

答案 0 :(得分:2)

FillCommand 

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

使用类似的东西:

using(SqlCommand command = FillCommand("usp_UpdateUser", _sqlConnection, _sqlTransaction, CommandType.StoredProcedure))
{
   ...
}