oracle 如何使用单个连接执行两个 command.ExecuteNonQuery() 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3667362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute two command.ExecuteNonQuery() methods using single connection?
提问by Shekhar
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.
我正在尝试调用 OracleCommand 类的两个不同对象的 ExecuteNonQuery() 方法。这两个对象使用相同的连接对象,但具有不同的 commandText 和参数。我正在使用 ODP.net、C#(.net 2.0 框架)和 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?
在执行上面两条语句之前,我正在检查连接是否打开。如果它没有打开,那么我正在打开连接。我的问题是,在command.ExecuteNonQuery();
执行后,连接被关闭,当控件尝试执行第二个语句时,我得到“连接必须打开才能执行此操作”的异常。为什么执行 ExecuteNonQuery() 方法后连接会自动关闭?
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 !!
** 只是想知道,打开和关闭连接的最佳做法是什么?我们应该在每个 ExecuteNonQuery()、ExecuteScalar() 等处打开连接吗?方法和关闭连接只要完成或在应用程序启动时打开连接并保持连接打开直到应用程序结束?请赐教!!
回答by Winston Smith
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.
这没有任何意义。如果您没有明确使用事务,则会自动提交更改。
回答by Ian Johnson
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();
}