CSFramework.EF 数据库框架 - 使用 BeginTransation 方法启动事务
C# 全选
//
//测试事务2,www.csframework.com
//
string connString = "Server=127.0.0.1;Database=CSFrameworkV6_Normal;User ID=demo_webapi;Password=test; Max Pool Size=512; Pooling=true;Connect Timeout=30;TrustServerCertificate=true;";
IDatabase db = DatabaseFactory.GetDatabase(DatabaseType.SqlServer, connString);
var list = db.GetQueryable<tb_PO>().OrderByDescending(o => o.PODate).Take(10).ToList();
var result = "";
try
{
db.BeginTransaction();
//修改第1条
var obj = list.First();
obj.Remark = "RunTransaction修改了:" + obj.Remark;
result += db.Update<tb_PO>(obj);
//删除最后1条
result += db.Remove(list.Last());
//add
var newObj = new tb_PO();
newObj.ISID = IdHelper.GetId();
newObj.PODate = DateTime.Now;
newObj.PONO = DocNoHelper.GetDocNo(db, "EF", 8, "admin");
newObj.CustomerCode = DocNoHelper.GetDataSN(db, "EF", true, 8, "-");
newObj.Remark = DocNoHelper.GetDataSN(db, "DB", true, 8, "-");
result += db.Add<tb_PO>(newObj);
//抛出异常
int div = 0;
var r = 1 / div;
db.CommitTransaction();
}
catch
{
db.RollbackTransaction();
throw;
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
CSFramework.EF C/S框架网