mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-16 16:50:41 +08:00
-
This commit is contained in:
parent
ba12acef8c
commit
7933b88a22
Binary file not shown.
@ -30,18 +30,30 @@ namespace OrmTest.Demo
|
||||
{
|
||||
var db = GetInstance();
|
||||
//1. no result
|
||||
db.UseStoredProcedure(() =>
|
||||
db.Ado.UseStoredProcedure(() =>
|
||||
{
|
||||
string spName = "sp_help";
|
||||
var getSpReslut = db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
|
||||
});
|
||||
|
||||
//2. has result
|
||||
var result= db.UseStoredProcedure<dynamic>(() =>
|
||||
var result= db.Ado.UseStoredProcedure<dynamic>(() =>
|
||||
{
|
||||
string spName = "sp_help";
|
||||
return db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
|
||||
});
|
||||
|
||||
//2. has output
|
||||
object outPutValue;
|
||||
var outputResult = db.Ado.UseStoredProcedure<dynamic>(() =>
|
||||
{
|
||||
string spName = "sp_school";
|
||||
var p1 = new SugarParameter("@p1", "1");
|
||||
var p2= new SugarParameter("@p2", null,true);//isOutput=true
|
||||
var dbResult= db.Ado.SqlQueryDynamic(spName,new SugarParameter[] {p1,p2 });
|
||||
outPutValue = p2.Value;
|
||||
return dbResult;
|
||||
});
|
||||
}
|
||||
|
||||
private static void Tran()
|
||||
@ -49,7 +61,7 @@ namespace OrmTest.Demo
|
||||
var db = GetInstance();
|
||||
|
||||
//1. no result
|
||||
var result = db.UseTran(() =>
|
||||
var result = db.Ado.UseTran(() =>
|
||||
{
|
||||
var beginCount = db.Queryable<Student>().Count();
|
||||
db.Ado.ExecuteCommand("delete student");
|
||||
@ -59,7 +71,7 @@ namespace OrmTest.Demo
|
||||
var count = db.Queryable<Student>().Count();
|
||||
|
||||
//2 has result
|
||||
var result2 = db.UseTran<List<Student>>(() =>
|
||||
var result2 = db.Ado.UseTran<List<Student>>(() =>
|
||||
{
|
||||
return db.Queryable<Student>().ToList();
|
||||
});
|
||||
|
@ -16,16 +16,12 @@ namespace OrmTest.UnitTest
|
||||
}
|
||||
public void Init()
|
||||
{
|
||||
|
||||
//IsAutoCloseConnection
|
||||
for (int i = 0; i < this.Count; i++)
|
||||
{
|
||||
var db = GetInstance();
|
||||
var x = db.Queryable<Student>().ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
|
@ -12,16 +12,12 @@ namespace OrmTest.UnitTest
|
||||
{
|
||||
public void Init()
|
||||
{
|
||||
|
||||
//IsAutoCloseConnection
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
var db = GetInstance();
|
||||
var x = db.Queryable<Student>().ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
public SqlSugarClient GetInstance()
|
||||
{
|
||||
|
@ -138,6 +138,76 @@ namespace SqlSugar
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
||||
return count;
|
||||
}
|
||||
|
||||
#region Use
|
||||
public SugarMessageResult<bool> UseTran(Action action)
|
||||
{
|
||||
var result = new SugarMessageResult<bool>();
|
||||
try
|
||||
{
|
||||
this.BeginTran();
|
||||
if (action != null)
|
||||
action();
|
||||
this.CommitTran();
|
||||
result.Data = result.IsSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Exception = ex;
|
||||
result.Messaage = ex.Message;
|
||||
result.IsSuccess = false;
|
||||
this.RollbackTran();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public SugarMessageResult<T> UseTran<T>(Func<T> action)
|
||||
{
|
||||
var result = new SugarMessageResult<T>();
|
||||
try
|
||||
{
|
||||
this.BeginTran();
|
||||
if (action != null)
|
||||
result.Data = action();
|
||||
this.CommitTran();
|
||||
result.IsSuccess = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Exception = ex;
|
||||
result.Messaage = ex.Message;
|
||||
result.IsSuccess = false;
|
||||
this.RollbackTran();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public void UseStoredProcedure(Action action)
|
||||
{
|
||||
var oldCommandType = this.CommandType;
|
||||
this.CommandType = CommandType.StoredProcedure;
|
||||
this.IsClearParameters = false;
|
||||
if (action != null)
|
||||
{
|
||||
action();
|
||||
}
|
||||
this.CommandType = oldCommandType;
|
||||
this.IsClearParameters = true;
|
||||
}
|
||||
public T UseStoredProcedure<T>(Func<T> action)
|
||||
{
|
||||
T result = default(T);
|
||||
var oldCommandType = this.CommandType;
|
||||
this.CommandType = CommandType.StoredProcedure;
|
||||
this.IsClearParameters = false;
|
||||
if (action != null)
|
||||
{
|
||||
result = action();
|
||||
}
|
||||
this.CommandType = oldCommandType;
|
||||
this.IsClearParameters = true;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual IDataReader GetDataReader(string sql, params SugarParameter[] pars)
|
||||
{
|
||||
base.SetParSize(pars);
|
||||
|
@ -14,7 +14,14 @@ namespace SqlSugar
|
||||
this.Value = value;
|
||||
this.ParameterName = name;
|
||||
}
|
||||
|
||||
public SugarParameter(string name, object value,bool isOutput)
|
||||
{
|
||||
this.Value = value;
|
||||
this.ParameterName = name;
|
||||
if (isOutput) {
|
||||
this.Direction = ParameterDirection.Output;
|
||||
}
|
||||
}
|
||||
public override System.Data.DbType DbType
|
||||
{
|
||||
get; set;
|
||||
@ -74,7 +81,5 @@ namespace SqlSugar
|
||||
{
|
||||
this.DbType = System.Data.DbType.String;
|
||||
}
|
||||
|
||||
public bool IsOutput { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -22,28 +22,28 @@ namespace SqlSugar
|
||||
ConnectionConfig MasterConnectionConfig { get; set; }
|
||||
List<ConnectionConfig> SlaveConnectionConfigs { get; set; }
|
||||
|
||||
CommandType CommandType { get; set; }
|
||||
CommandType CommandType { get; set; }
|
||||
bool IsEnableLogEvent { get; set; }
|
||||
Action<string, string> LogEventStarting { get; set; }
|
||||
Action<string, string> LogEventCompleted { get; set; }
|
||||
bool IsClearParameters { get; set; }
|
||||
int CommandTimeOut { get; set; }
|
||||
IDbBind DbBind { get; }
|
||||
void SetCommandToAdapter(IDataAdapter adapter,IDbCommand command);
|
||||
void SetCommandToAdapter(IDataAdapter adapter, IDbCommand command);
|
||||
IDataAdapter GetAdapter();
|
||||
IDbCommand GetCommand(string sql, SugarParameter[] parameters);
|
||||
DataTable GetDataTable(string sql, object parameters);
|
||||
DataTable GetDataTable(string sql, params SugarParameter[] parameters);
|
||||
DataTable GetDataTable(string sql, List<SugarParameter> [] parameters);
|
||||
DataTable GetDataTable(string sql, List<SugarParameter>[] parameters);
|
||||
DataSet GetDataSetAll(string sql, object parameters);
|
||||
DataSet GetDataSetAll(string sql, params SugarParameter[] parameters);
|
||||
DataSet GetDataSetAll(string sql, List<SugarParameter> parameters);
|
||||
IDataReader GetDataReader(string sql,object parameters);
|
||||
IDataReader GetDataReader(string sql, object parameters);
|
||||
IDataReader GetDataReader(string sql, params SugarParameter[] parameters);
|
||||
IDataReader GetDataReader(string sql,List<SugarParameter> parameters);
|
||||
IDataReader GetDataReader(string sql, List<SugarParameter> parameters);
|
||||
object GetScalar(string sql, object parameters);
|
||||
object GetScalar(string sql, params SugarParameter[] parameters);
|
||||
object GetScalar(string sql,List<SugarParameter> parameters);
|
||||
object GetScalar(string sql, List<SugarParameter> parameters);
|
||||
int ExecuteCommand(string sql, object parameters);
|
||||
int ExecuteCommand(string sql, params SugarParameter[] parameters);
|
||||
int ExecuteCommand(string sql, List<SugarParameter> parameters);
|
||||
@ -53,9 +53,9 @@ namespace SqlSugar
|
||||
int GetInt(string sql, object pars);
|
||||
int GetInt(string sql, params SugarParameter[] parameters);
|
||||
int GetInt(string sql, List<SugarParameter> parameters);
|
||||
Double GetDouble(string sql,object parameters);
|
||||
Double GetDouble(string sql, object parameters);
|
||||
Double GetDouble(string sql, params SugarParameter[] parameters);
|
||||
Double GetDouble(string sql,List<SugarParameter> parameters);
|
||||
Double GetDouble(string sql, List<SugarParameter> parameters);
|
||||
decimal GetDecimal(string sql, object parameters);
|
||||
decimal GetDecimal(string sql, params SugarParameter[] parameters);
|
||||
decimal GetDecimal(string sql, List<SugarParameter> parameters);
|
||||
@ -76,11 +76,16 @@ namespace SqlSugar
|
||||
void Open();
|
||||
void CheckConnection();
|
||||
|
||||
void BeginTran();
|
||||
void BeginTran(IsolationLevel iso);
|
||||
void BeginTran(string transactionName);
|
||||
void BeginTran(IsolationLevel iso, string transactionName);
|
||||
void RollbackTran();
|
||||
void CommitTran();
|
||||
void BeginTran();
|
||||
void BeginTran(IsolationLevel iso);
|
||||
void BeginTran(string transactionName);
|
||||
void BeginTran(IsolationLevel iso, string transactionName);
|
||||
void RollbackTran();
|
||||
void CommitTran();
|
||||
|
||||
SugarMessageResult<bool> UseTran(Action action);
|
||||
SugarMessageResult<T> UseTran<T>(Func<T> action);
|
||||
void UseStoredProcedure(Action action);
|
||||
T UseStoredProcedure<T>(Func<T> action);
|
||||
}
|
||||
}
|
||||
|
@ -85,9 +85,7 @@ namespace SqlSugar
|
||||
p.Size = paramter.Size;
|
||||
p.Value = paramter.Value;
|
||||
p.DbType = paramter.DbType;
|
||||
if (paramter.IsOutput) {
|
||||
p.Direction = ParameterDirection.Output;
|
||||
}
|
||||
p.Direction = paramter.Direction;
|
||||
result[i] =p;
|
||||
++i;
|
||||
}
|
||||
|
@ -87,8 +87,7 @@ namespace SqlSugar
|
||||
p.Size = parameter.Size;
|
||||
p.Value = parameter.Value;
|
||||
p.DbType = parameter.DbType;
|
||||
if (parameter.IsOutput)
|
||||
p.Direction = ParameterDirection.Output;
|
||||
p.Direction = parameter.Direction;
|
||||
result[i] = p;
|
||||
++i;
|
||||
}
|
||||
|
@ -306,74 +306,5 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
#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;
|
||||
}
|
||||
public void UseStoredProcedure(Action action)
|
||||
{
|
||||
var oldCommandType = this.Ado.CommandType;
|
||||
this.Ado.CommandType = CommandType.StoredProcedure;
|
||||
Ado.IsClearParameters = false;
|
||||
if (action != null)
|
||||
{
|
||||
action();
|
||||
}
|
||||
this.Ado.CommandType = oldCommandType;
|
||||
Ado.IsClearParameters = true;
|
||||
}
|
||||
public T UseStoredProcedure<T>(Func<T> action)
|
||||
{
|
||||
T result = default(T);
|
||||
var oldCommandType = this.Ado.CommandType;
|
||||
this.Ado.CommandType = CommandType.StoredProcedure;
|
||||
Ado.IsClearParameters = false;
|
||||
if (action != null)
|
||||
{
|
||||
result = action();
|
||||
}
|
||||
this.Ado.CommandType = oldCommandType;
|
||||
Ado.IsClearParameters = true;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user