2013-01-08 55 views
0

我想用usingsql command如何在fillCommand中使用?

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

什么是FillCommand在这种情况下?这是你写的一个函数吗? –

+0

是FillCommand是我的功能 – lakki

+0

@lakki LOL,trippino要求您发布您正在使用的'FillCommand'的定义。它可能不是'void FillCommand(ref SqlCommand command){...}'; – Candide

回答

2

由于

FillCommand 

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

使用这样的事情:

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

我的想法也是。然后你只需在using语句中调用FillCommand。 – mortb

+0

是的,功能必须采取所有参数来实例化当然的整个命令 –