TransactionScope()事务的问题
TransactionScope()事务的问题
using (TransactionScope ts = new TransactionScope())
{
}=>这里发生了错误,错误信息:已中止的事务
请教的微软工程师,得到的答案是:
控制与登记的 System.Transactions 事务关联的连接。
可能的值包括:
Transaction Binding=Implicit Unbind;
Transaction Binding=Explicit Unbind;
Implicit Unbind 可使连接在事务结束时从事务中分离。分离后,连接上的其他请求将以自动提交模式执行。
在事务处于活动状态的情况下执行请求时,不会检查 System.Transactions.Transaction.Current 属性。
事务结束后,其他请求将以自动提交模式执行。
Explicit Unbind 可使连接保持连接到事务,直到连接关闭或调用显式 SqlConnection.TransactionEnlist(null)。
如果 Transaction.Current 不是登记的事务或登记的事务未处于活动状态,则引发 InvalidOperationExceptio
又:
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required,new TimeSpan(1,1,0)))
传统的事务写法:
using (SqlConnection conn = new SqlConnection(@"xxx"))
{
}
SqlConnection myConnection = new SqlConnection(DataBaseOperation.strSqlConDataBaseOperation);
myConnection.Open();
SqlTransaction myTrans;
myTrans = myConnection.BeginTransaction();
SqlDataAdapter sqldataadapter = new SqlDataAdapter( "Select * From ProPlaDocuments_details ",myConnection);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(sqldataadapter);
sqldataadapter.SelectCommand = new SqlCommand( "Select * From ProPlaDocuments_details ", myConnection, myTrans);
sqldataadapter.InsertCommand = new SqlCommand( " ", myConnection, myTrans);
sqldataadapter.UpdateCommand = new SqlCommand( " ", myConnection, myTrans);
sqldataadapter.DeleteCommand = new SqlCommand( " ", myConnection, myTrans);
sqldataadapter.InsertCommand = myCommandBuilder.GetInsertCommand();
sqldataadapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
sqldataadapter.DeleteCommand = myCommandBuilder.GetDeleteCommand();
try
{
sqldataadapter.Update(dTableProPlaDocuments_details);
myTrans.Commit();
}
catch...