Linq to sql 事務處理

2021-06-17 22:28:44 字數 824 閱讀 4177

linq to sql 在提交更新的時候缺省會建立事務,一部分修改發生錯誤的話其它修改也不

會生效:

ctx.customers.add(new customer );

ctx.customers.add(new customer );

ctx.submitchanges();

假設資料庫中已經存在顧客 id 為「abcde」的記錄,那麼第二次插入操作失敗將會導致第

一次的插入操作失效。執行程式後會得到乙個異常,查詢資料庫發現「abcdf」這個顧客也沒

有插入到資料庫中。

如果每次更新後直接提交修改,那麼我們可以使用下面的方式做事務:

if (ctx.connection != null) ctx.connection.open();

dbtransaction tran = ctx.connection.begintransaction();

ctx.transaction = tran;

try);

createcustomer(new customer );

tran.commit(); 

} catch

private void createcustomer(customer c)

執行程式後發現增加顧客 abcdf 的操作並沒有成功。或者,我們還可以通過

transactionscope 實現事務:

using (transactionscope scope = new transactionscope())

);createcustomer(new customer );

scope.complete();

PB事務處理

1 資料視窗更新,只要dberror有錯誤,而事先沒有做過任何commit工作,那麼rollback可以回滾到上次commit位置,即上次commit後所有的資料將被回滾。2 如果是直接寫入sql語句,只要資料庫出現錯誤,那麼rollback可以回滾到上次commit的位置,即上次commit後所有...

MySQL事務處理

start transaction,commit和rollback語法 start transaction begin work commit work and no chain no release rollback work and no chain no release set autocom...

ASP事務處理

asp事務處理。測試資料庫為sql server,伺服器為本機,資料庫名為test,表名為a,兩個欄位id int 主鍵標識,num int set conn server.createobject adodb.connection strconn provider sqloledb.1 persi...