Yes. It will dispose of your object. This will actually cause a problem in your code, since the SqlCommand
being returned is dependent on the SqlConnection
, which will be Disposed of prior to the control flow returning to your caller.
You can, however, use delegates to work around this. A good pattern to handle this is to rewrite your method like so:
public static SqlCommand ProcessSqlCommand(string strSql, string strConnect, Action<SqlCommand> processingMethod)
{
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
SqlCommand cmd = GetSqlCommand();
cmd.Connection = con;
cmd.CommandText = strSql;
processingMethod(cmd);
}
}
You can then call this like:
ProcessSqlCommand(sqlStr, connectStr, (cmd) =>
{
// Process the cmd results here...
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…