SQLHelper类-ExecuteNonQuery代码修改

发布于 2024-07-25 09:33:53 字数 1084 浏览 1 评论 0原文

我只是浏览 SQLHelper 类 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”中是否有原因? 我在代码的其他地方看到了“使用块”的使用。

I am just browsing the code in the SQLHelper Class V2 and I notice the following

    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;
    }

Is there a reason why the command wasn't within a "Using Bolck"? I see the use of the"Using Block" elsewhere in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2024-08-01 09:33:53

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

My guess is that its because the command is used throughout the scope of the function, so it would just add extra code. Different developer preferring different implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文