使用ADO.net,如果我填充數據適配器而不隱式地執行BeginTransaction,是否會發生「事務」?如果不是,那麼默認的隔離級別是什麼?ADO.NET事務
0
A
回答
1
DataAdapter沒有隱含事務。
1
要在您的數據適配器上運行事務,只需使用TransactionScope。
using (var ts = new TransactionScope())
{
// do your data adapter related code
// ...
ts.Complete();
}
當使用TransactionScope時,您也可以指定IsolationLevel(例如Serializable)。 TransactionScope上的默認隔離級別是Serializable。
0
您將需要顯式創建一個事務。與TransactionScope一起,您可以使用SqlTransaction。
例如:
var connString = "Data Source=(local);Integrated security=SSPI; Initial Catalog=Northwind";
var sql = "SELECT * FROM Orders";
using (var conn = new SqlConnection(connString))
{
conn.Open();
var da = new SqlDataAdapter(sql, conn);
var ds = new DataSet();
// load data from the data source into the DataSet
da.Fill(ds, "Orders");
// start the transaction
SqlTransaction tran = conn.BeginTransaction();
// associate transaction with the data adapter command objects
da.DeleteCommand.Transaction = tran;
da.InsertCommand.Transaction = tran;
da.UpdateCommand.Transaction = tran;
// modify the data in the DataSet
// submit changes, commit or rollback, and close the connection
try
{
da.Update(ds, "Orders");
// commit if successful
tran.Commit();
}
catch (Exception)
{
tran.Rollback();
}
}
相關問題
- 1. ADO.NET事務和SQL Server 2008事務
- 2. ADO.NET中的嵌套事務
- 3. ADO.Net實體框架事務
- 4. ADO.Net事務不起作用
- 5. ADO.NET:你需要回滾事務嗎?
- 6. 在TransactionScope中使用ADO.Net事務?
- 7. ADO.NET 2.0之前的分佈式事務
- 8. 在ADO.NET事務中使用using()塊
- 9. 使用ADO.net中的事務SQL
- 10. ADO.NET數據服務
- 11. ADO.NET數據服務
- 12. Ado.net數據服務
- 13. ADO.NET全球OnBeforeExecute事件
- 14. C#通過ADO.NET失敗的Oracle和SQL Server事務
- 15. 使用EntityFramework和ado.net存儲過程的相同事務
- 16. LINQ-SQL&ADO.NET - 如何使批量事務完全異步?
- 17. 爲什麼客戶端啓動的Ado.Net事務不允許...?
- 18. 在單個ado.net事務中執行多個語句/ sqlco命令
- 19. 僅在單個ADO.NET事務中更改隔離級別
- 20. 做NHibernate的事務減慢其他ADO.NET連接?
- 21. ADO.NET重試一個sql連接內的事務
- 22. 多插入事務連接超時 - ADO.NET - SQL Server
- 23. MSDTC問題與ADO.NET實體框架中的事務
- 24. ADO.NET SQL事務 - 只有1個調用數據庫?
- 25. SQL Server和ADO.NET中的事務之間的區別?
- 26. 等待ADO.NET或TSQL中的事務複製
- 27. ADO.NET中寫入的db事務何時實際開始?
- 28. 在Ado.Net更新winth事務「併發衝突」
- 29. DBServerSyncProvider SelectClientIdCommand同步服務Ado.Net
- 30. ADO.net數據服務 - 查詢