问题描述
我很困惑.从表面上看,似乎在C#中执行事务简单的.从这里:
http://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleTransactionClass.htm
string constr = "User Id=scott;Password=tiger;Data Source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
// Start a transaction
OracleTransaction txn = con.BeginTransaction(
IsolationLevel.ReadCommitted);
try
{
// Insert the same row twice into MyTable
cmd.CommandText = "INSERT INTO MyTable VALUES (1)";
cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery(); // This may throw an exception
txn.Commit();
}....
因此,创建一个连接,在该连接上开始事务,然后关闭,直到要提交或回滚为止.
但是,其他来源,例如:
https://forums.oracle.com/thread/319121
提倡设置OracleCommand对象本身的Transaction属性.例如cmd.Transaction = txn;
另一些消息来源说此属性是只读的.其实没看过只是,但似乎没有地方清楚地说出它的作用.
因此,我的困惑是交易的存在OracleCommand对象上的属性似乎建议它应该用于在交易中执行该命令,但是Oracle自己的文档不使用此属性.那是什么为?
所以我的问题是:
- 我是否需要设置OracleCommand的Transaction属性,并且如果是这样,这到底是做什么的?
- 如果我已经开始进行连接上的交易,那么以后都可以吗?即使未设置事务,也会在该事务的该连接(直到提交或回滚)部分上执行命令这些命令的属性?
否.
这是无人操作.
OracleCommand
自动重用"命令的OracleConnection
上当前处于活动状态的事务. Transaction
属性之所以存在,仅仅是因为它是在基类(DbCommand
)中声明的,并且您不能在继承的类中取消声明"成员.如果您阅读它,将会得到连接的事务(如果有的话),将其设置为无效.
完全正确.
I'm confused. On the face of it, performing a transaction in C# seemssimple. From here:
http://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleTransactionClass.htm
string constr = "User Id=scott;Password=tiger;Data Source=oracle";
OracleConnection con = new OracleConnection(constr);
con.Open();
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM MyTable";
// Start a transaction
OracleTransaction txn = con.BeginTransaction(
IsolationLevel.ReadCommitted);
try
{
// Insert the same row twice into MyTable
cmd.CommandText = "INSERT INTO MyTable VALUES (1)";
cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery(); // This may throw an exception
txn.Commit();
}....
So, create a connection, begin a transaction on that connection, and then off you go until you want to commit or rollback.
However, other sources, such as here:
https://forums.oracle.com/thread/319121
advocate setting the Transaction property of the OracleCommand object itself. e.g.
cmd.Transaction = txn;
Yet other sources say that this property is read only. It's not actually readonly, but nowhere appears to clearly say what it does.
My confusion, therefore, is that the existence of the Transactionproperty on the OracleCommand object seems to suggest that it shouldbe used to perform that command as part of a transaction, and yetOracle's own documentation does not use this property. So what is itfor?
So my questions are:
- do I need to set the Transaction property of my OracleCommand, andif so, what exactly does this do?
- If I've started a transaction on a connection, are ALL subsequentcommands performed on that connection (until a commit or rollback) part of that transaction, even if I don't set the Transactionproperty on those commands?
No.
It's a no-op.
The OracleCommand
automatically "reuses" the transaction that is currently active on the command's OracleConnection
. The Transaction
property is there simply because it was declared in the base class (DbCommand
) and you cannot "undeclare" a member in the inherited class. If you read it you'll get the connection's transaction (if any), setting it does nothing.
Exactly.
这篇关于使用C#和ODP.NET执行Oracle事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!