SQLHelper类 - ExecuteNonQuery代码修改

时间:2009-06-26 20:09:56

标签: .net sqlhelper

我只是浏览SQLHelper Class V2中的代码,我注意到以下内容

    public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
        if( transaction == null ) throw new ArgumentNullException( "transaction" );
        if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rollbacked or commited, please provide an open transaction.", "transaction" );

        // Create a command and prepare it for execution
        SqlCommand cmd = new SqlCommand();
        bool mustCloseConnection = false;
        PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );

        // Finally, execute the command
        int retval = cmd.ExecuteNonQuery();

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

有没有理由说命令不在“使用Bolck”中?我在代码中的其他地方看到了“使用块”的使用。

1 个答案:

答案 0 :(得分:0)

我的猜测是因为该命令在整个函数范围内使用,所以它只会添加额外的代码。不同的开发人员更喜欢不同的实现。