简体   繁体   中英

What is the best practice dealing with SqlCommand [on hold]

I came across this sample code, which I have modified a little bit.

public static int ExecuteNonQuery(SqlConnection conn, CommandType commandType, 
    string commandText, params SqlParameter[] commandParameters)
{
    SqlCommand cmd = new SqlCommand();

    PrepareCommand(cmd, conn, (SqlTransaction)null, commandType, commandText, 
                   commandParameters);

    int ret = cmd.ExecuteNonQuery();

    // detach the SqlParameters from the command object, so they can be used again.
    cmd.Parameters.Clear();

    return ret;
}

However, I have a concern here in the above code.

That is, if the ExecuteNonQuery is getting called multiple times from the UI, there will be multiple new objects getting created.

I understand, .NET has auto garbage collection; however, should not the cmd be explicitly taken care in the code?

So, I am thinking to modify this code like below. Does the below modification, follow best practice?

public static int ExecuteNonQuery(SqlConnection conn, CommandType commandType, 
    string commandText, params SqlParameter[] commandParameters)
{
    using(SqlCommand cmd = new SqlCommand())
    {
        PrepareCommand(cmd, conn, (SqlTransaction)null, commandType, 
                       commandText, commandParameters);
        int ret = cmd.ExecuteNonQuery();

        // detach the SqlParameters from the command object, so they can be 
        //  used again.
        cmd.Parameters.Clear();
    }

    return ret;
}

Here are few other questions I have:

  1. cmd.Parameters.Clear(); - will this statement clean up the new object? Or it will just cleanup the parameter (that is, content inside the object)?

  2. If my modification follows the best practice, will the modified code work perfectly as previous code?

cmd.Parameters.Clear(); //will this statement clean up the new object? Or it will just cleanup the parameter( that is, content inside the object)?

I'm not exactly sure what you're asking, but it will not "clean up" anything. Only the garbage collector cleans up objects, which happens when nothing will be referencing them anymore . Setting local variables to null will do nothing to make garbage cleanup any more efficient.

If my modification follows the best practice, will the modified code work perfectly as previous code?

Functionally it is the same as the original block. The only difference is that the command is disposed of.

Note that whatever calls this method should dispose of the connection as soon as it's done with it. It's not a best practice to keep a single connection object open and re-used. Connections are pooled by the framework so re-creating them generally isn't an expensive process, but leaving an unused connection open can cause issues.

If I were reviewing your code (which I guess I am) I would just scrap this method all together. There's no need to clear the Parameters property, and you can create the command and connection consecutively, disposing of them when the command finishes:

using(SqlConnection conn = Getconnection())
using(SqlCommand cmd = new SqlCommand(commandText, conn) 
{
    cmd.CommandType = commandType;
    cmd.Parameters = commandParameters;

    cmd.ExecuteNonQuery();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM