南骏 池
2025-05-16 a04ee7ab3b4cb4e4bb73cda632233f043e7422f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using NewPdaSqlServer.util;
using SqlSugar;
 
namespace NewPdaSqlServer.DB;
 
public class RepositoryNoEntity
{
    protected static SqlSugarScope Db = new(new ConnectionConfig
        {
            DbType = DbType.SqlServer,
            ConnectionString = AppsettingsUtility.Settings.DataBaseConn,
            IsAutoCloseConnection = true,
            MoreSettings = new ConnMoreSettings
            {
                IsNoReadXmlDescription = true // 设置忽略读取XML描述
            }
        },
        db =>
        {
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(
                    UtilMethods.GetSqlString(DbType.SqlServer, sql, pars));
            };
        });
 
 
    protected int UseTransaction(Func<SqlSugarScope, int> action)
    {
        try
        {
            Db.Ado.BeginTran(); // 开始事务
            var affectedRows = action.Invoke(Db); // 执行传入的操作并获取受影响的行数
            Db.Ado.CommitTran(); // 提交事务
            return affectedRows; // 返回受影响的行数
        }
        catch (Exception)
        {
            Db.Ado.RollbackTran(); // 回滚事务
            throw; // 重新抛出异常
        }
    }
}