Yes. It will destroy your object. This will actually cause a problem in your code, as the returned SqlCommand depends on the SqlConnection , which will be deleted before the control flow returns to your caller.
However, you can use delegates to get around this. A good example for this is to rewrite your method as follows:
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); } }
Then you can call it like this:
ProcessSqlCommand(sqlStr, connectStr, (cmd) => { // Process the cmd results here... });
source share