mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 02:29:39 +08:00
-
This commit is contained in:
@@ -22,6 +22,23 @@ namespace OrmTest.Demo
|
|||||||
Ado();
|
Ado();
|
||||||
Group();
|
Group();
|
||||||
Sqlable();
|
Sqlable();
|
||||||
|
Tran();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Tran()
|
||||||
|
{
|
||||||
|
var db = GetInstance();
|
||||||
|
|
||||||
|
var result=db.UseTran(() =>
|
||||||
|
{
|
||||||
|
var count= db.Ado.ExecuteCommand("delete student");
|
||||||
|
throw new Exception("error haha");
|
||||||
|
});
|
||||||
|
|
||||||
|
var result2 = db.UseTran<List<Student>>(() =>
|
||||||
|
{
|
||||||
|
return db.Queryable<Student>().ToList();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Group()
|
private static void Group()
|
||||||
|
@@ -86,11 +86,13 @@ namespace SqlSugar
|
|||||||
#region Transaction
|
#region Transaction
|
||||||
public virtual void BeginTran()
|
public virtual void BeginTran()
|
||||||
{
|
{
|
||||||
this.Connection.BeginTransaction();
|
CheckConnection();
|
||||||
|
this.Transaction=this.Connection.BeginTransaction();
|
||||||
}
|
}
|
||||||
public virtual void BeginTran(IsolationLevel iso)
|
public virtual void BeginTran(IsolationLevel iso)
|
||||||
{
|
{
|
||||||
this.Connection.BeginTransaction(iso);
|
CheckConnection();
|
||||||
|
this.Transaction = this.Connection.BeginTransaction(iso);
|
||||||
}
|
}
|
||||||
public virtual void RollbackTran()
|
public virtual void RollbackTran()
|
||||||
{
|
{
|
||||||
|
15
SqlSugar/Entities/SugarMessageResult.cs
Normal file
15
SqlSugar/Entities/SugarMessageResult.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace SqlSugar
|
||||||
|
{
|
||||||
|
public class SugarMessageResult<T>
|
||||||
|
{
|
||||||
|
public bool IsSuccess { get; set; }
|
||||||
|
public Exception Exception { get; set; }
|
||||||
|
public string Messaage { get; set; }
|
||||||
|
public T Data { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@@ -58,6 +58,7 @@
|
|||||||
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
|
||||||
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />
|
||||||
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
|
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
|
||||||
|
<Compile Include="Entities\SugarMessageResult.cs" />
|
||||||
<Compile Include="Interface\IConnectionConfig.cs" />
|
<Compile Include="Interface\IConnectionConfig.cs" />
|
||||||
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerDeleteBuilder.cs" />
|
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerDeleteBuilder.cs" />
|
||||||
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerInsertBuilder.cs" />
|
<Compile Include="Realization\SqlServer\Core\SqlBuilder\SqlServerInsertBuilder.cs" />
|
||||||
|
@@ -385,5 +385,48 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Use Methods
|
||||||
|
public SugarMessageResult<bool> UseTran(Action action)
|
||||||
|
{
|
||||||
|
var result = new SugarMessageResult<bool>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Ado.BeginTran();
|
||||||
|
if (action != null)
|
||||||
|
action();
|
||||||
|
this.Ado.CommitTran();
|
||||||
|
result.Data = result.IsSuccess = true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
result.Exception = ex;
|
||||||
|
result.Messaage = ex.Message;
|
||||||
|
result.IsSuccess = false;
|
||||||
|
this.Ado.RollbackTran();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public SugarMessageResult<T> UseTran<T>(Func<T> action)
|
||||||
|
{
|
||||||
|
var result = new SugarMessageResult<T>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.Ado.BeginTran();
|
||||||
|
if (action != null)
|
||||||
|
result.Data = action();
|
||||||
|
this.Ado.CommitTran();
|
||||||
|
result.IsSuccess = true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
result.Exception = ex;
|
||||||
|
result.Messaage = ex.Message;
|
||||||
|
result.IsSuccess = false;
|
||||||
|
this.Ado.RollbackTran();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user