如何使用单个连接执行两个 command.ExecuteNonQuery() 方法?

发布于 2024-09-17 20:13:57 字数 833 浏览 3 评论 0原文

我试图调用 OracleCommand 类的两个不同对象的 ExecuteNonQuery() 方法。这两个对象使用相同的连接对象,但具有不同的命令文本和参数。我正在使用 ODP.net、C#(.net 2.0 框架)和 Oracle 10g。

代码片段如下:

// OpenDatabaseConnection() methods checks and opens database connection
bool connectionOpened = OpenDatabaseConnection();
if (connectionOpened)
{
    command.ExecuteNonQuery();
    commitCommand.ExecuteNonQuery();
}

在执行上面两条语句之前,我检查连接是否打开。如果没有打开,那么我打开连接。 我的问题是,在执行 command.ExecuteNonQuery(); 后,连接被关闭,并且当控件尝试执行第二条语句时,我收到“连接必须打开才能执行此操作”的异常。为什么执行 ExecuteNonQuery() 方法后连接会自动关闭?

谁能告诉我如何解决这种情况? 在第二个命令对象中,我只是尝试提交更改,仅此而已。如何在不使用事务的情况下提交更改?

提前致谢

**

编辑

** 只是想知道打开和关闭连接的最佳实践是什么?我们应该在每个 ExecuteNonQuery()、ExecuteScalar() 等处打开连接吗?方法并关闭 connectio 只要完成或在应用程序启动时打开连接并保持连接打开直到应用程序结束?请赐教!!

I am trying to invoke ExecuteNonQuery() method of two different objects of OracleCommand class. Both the objects use same connection object but have different commandText and parameters. I am using ODP.net, C# (.net 2.0 framework) and Oracle 10g.

Code snippet is as follows :

// OpenDatabaseConnection() methods checks and opens database connection
bool connectionOpened = OpenDatabaseConnection();
if (connectionOpened)
{
    command.ExecuteNonQuery();
    commitCommand.ExecuteNonQuery();
}

Before executing above two statements, I am checking whether connection is open or not.If its not open, then I am opening connection.
My problem is, after command.ExecuteNonQuery(); gets executed, connection gets closed and I get exception of 'Connection must be open to perform this operation' when control tries to execute second statement. Why does connection gets close automatically after performing ExecuteNonQuery() method?

Can anyone please tell me how to tackle this situation?
In second command object, I am just trying to commit the changes, nothing else. How to commit changes without using transactions?

Thanks in Advance

**

EDIT

**
Just wanted to know, what is the best practice for opening and closing the connection? Shall we open connection at each ExecuteNonQuery(), ExecuteScalar(),etc. methods and close connectio as long as are done or open connection at application startup and keep the connection open until application ends? Please enlighten !!

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

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

发布评论

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

评论(2

看海 2024-09-24 20:13:57

如何在不使用的情况下提交更改
交易?

这没有任何意义。如果您没有显式使用事务,则会自动提交更改。

How to commit changes without using
transactions?

This doesn't make any sense. If you're not explicitly using a transaction, changes are committed automatically.

¢蛋碎的人ぎ生 2024-09-24 20:13:57

你的提交命令是什么?难道只是为了承担工作吗?如果是这样,您就不需要这样做,因为无论您是否喜欢,都会在运行第一个查询时隐式创建并提交事务。

如果两个查询都需要作为一个整体运行和提交,那么听起来您可能想要使用事务

using(var connection = new OracleConnection(connectionString))
{
    var firstCommand = new OracleCommand(firstCommandString);
    var secondCommand = new OracleCommand(secondCommandString);

    var transaction = connection.BeginTransaction("SampleTransaction");

    firstCommand.Connection = connection;
    firstCommand.Transaction = transaction;

    secondCommand.Connection = connection;
    secondCommand.Transaction = transaction;

    firstCommand.ExecuteNonQuery();
    secondCommand.ExecuteNonQuery();

    transaction.Commit();
}

What is your commit command? Is that just to commit the work? If so you would not need to do so as a transaction would be implicitly created and committed on running the first query whether you like it or not.

If both queries need to be run and committed as a whole then it sounds like you want to might want to use transactions

using(var connection = new OracleConnection(connectionString))
{
    var firstCommand = new OracleCommand(firstCommandString);
    var secondCommand = new OracleCommand(secondCommandString);

    var transaction = connection.BeginTransaction("SampleTransaction");

    firstCommand.Connection = connection;
    firstCommand.Transaction = transaction;

    secondCommand.Connection = connection;
    secondCommand.Transaction = transaction;

    firstCommand.ExecuteNonQuery();
    secondCommand.ExecuteNonQuery();

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