using MES.Service.util;
|
using SqlSugar;
|
|
namespace MES.Service.DB;
|
|
public class SqlSugarHelper
|
{
|
private static readonly string _connectionString =
|
AppsettingsUtility.Settings.DataBaseConn; // 这里替换成你的实际连接字符串
|
|
public static SqlSugarClient GetInstance()
|
{
|
var db = new SqlSugarClient(new ConnectionConfig
|
{
|
ConnectionString = _connectionString,
|
DbType = DbType.Oracle,
|
IsAutoCloseConnection = true, //开启自动释放模式和EF原理一样我就不多解释了
|
InitKeyType = InitKeyType.Attribute
|
});
|
|
|
//调式代码 用来打印SQL
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
{
|
Console.WriteLine(
|
UtilMethods.GetSqlString(DbType.Oracle, sql, pars));
|
};
|
return db;
|
}
|
|
public static int UseTransactionWithOracle(Func<SqlSugarClient, int> action)
|
{
|
using (var db = GetInstance())
|
{
|
try
|
{
|
db.Ado.BeginTran(); // 开始事务
|
var affectedRows = action.Invoke(db); // 执行传入的操作并获取受影响的行数
|
db.Ado.CommitTran(); // 提交事务
|
return affectedRows; // 返回受影响的行数
|
}
|
catch (Exception ex)
|
{
|
db.Ado.RollbackTran(); // 回滚事务
|
throw ex;
|
}
|
}
|
}
|
}
|