下面是 ALinq 使用事务的例子,比较简单,直接上代码:
var db = new AccessNorthwind("C:/Northwind.mdb") { Log = Console.Out }; db.DoTransaction(delegate() { db.Customers.Update(o => new Customer { CompanyName = "XXXX" }, o => o.CustomerID == "kkkkk"); db.Customers.Delete(o => o.CustomerID == "aaaaa"); });
public static partial class Utility { public static void DoTransaction(this DataContext dc, Action func) { dc.Connection.Open(); var tran = dc.Connection.BeginTransaction(); dc.Transaction = tran; try { func(); tran.Commit(); } catch { tran.Rollback(); throw; } finally { dc.Connection.Close(); } } }
本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/
有朋友问到,如果是用 SQL 的,怎么写, OK,再上一个 SQL 的例子:
var str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Northwind.mdb"; new OleDbConnection(str).DoTransaction(delegate(IDbCommand command) { command.CommandText = @"UPDATE [Customers] SET [CompanyName] = @p0 WHERE [CustomerID] = @p1"; command.Parameters.Add(new OleDbParameter("@p0", "XXXXX")); command.Parameters.Add(new OleDbParameter("@p1", "kkkkk")); command.ExecuteNonQuery(); command.CommandText = @"DELETE FROM [Customers] WHERE [CustomerID] = @p0"; command.Parameters.Add(new OleDbParameter("@p0", "aaaaa")); command.ExecuteNonQuery(); });
public static partial class Utility { public static void DoTransaction(this DataContext dc, Action func) { dc.Connection.Open(); var tran = dc.Connection.BeginTransaction(); dc.Transaction = tran; try { func(); tran.Commit(); } catch { tran.Rollback(); throw; } finally { dc.Connection.Close(); } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索command
, static
, connection
, oledbparameter
, public
, dc
ALinq
linq 事务、linq to sql 事务、linq 启用事务、linq max 如何使用、c linq 使用,以便于您获取更多的相关知识。
时间: 2025-01-03 11:02:50