This commit is contained in:
sunkaixuan
2017-05-28 11:10:11 +08:00
parent ba12acef8c
commit 7933b88a22
10 changed files with 115 additions and 103 deletions

Binary file not shown.

View File

@@ -30,18 +30,30 @@ namespace OrmTest.Demo
{ {
var db = GetInstance(); var db = GetInstance();
//1. no result //1. no result
db.UseStoredProcedure(() => db.Ado.UseStoredProcedure(() =>
{ {
string spName = "sp_help"; string spName = "sp_help";
var getSpReslut = db.Ado.SqlQueryDynamic(spName, new { objname = "student" }); var getSpReslut = db.Ado.SqlQueryDynamic(spName, new { objname = "student" });
}); });
//2. has result //2. has result
var result= db.UseStoredProcedure<dynamic>(() => var result= db.Ado.UseStoredProcedure<dynamic>(() =>
{ {
string spName = "sp_help"; string spName = "sp_help";
return db.Ado.SqlQueryDynamic(spName, new { objname = "student" }); 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() private static void Tran()
@@ -49,7 +61,7 @@ namespace OrmTest.Demo
var db = GetInstance(); var db = GetInstance();
//1. no result //1. no result
var result = db.UseTran(() => var result = db.Ado.UseTran(() =>
{ {
var beginCount = db.Queryable<Student>().Count(); var beginCount = db.Queryable<Student>().Count();
db.Ado.ExecuteCommand("delete student"); db.Ado.ExecuteCommand("delete student");
@@ -59,7 +71,7 @@ namespace OrmTest.Demo
var count = db.Queryable<Student>().Count(); var count = db.Queryable<Student>().Count();
//2 has result //2 has result
var result2 = db.UseTran<List<Student>>(() => var result2 = db.Ado.UseTran<List<Student>>(() =>
{ {
return db.Queryable<Student>().ToList(); return db.Queryable<Student>().ToList();
}); });

View File

@@ -16,16 +16,12 @@ namespace OrmTest.UnitTest
} }
public void Init() public void Init()
{ {
//IsAutoCloseConnection //IsAutoCloseConnection
for (int i = 0; i < this.Count; i++) for (int i = 0; i < this.Count; i++)
{ {
var db = GetInstance(); var db = GetInstance();
var x = db.Queryable<Student>().ToList(); var x = db.Queryable<Student>().ToList();
} }
} }
public SqlSugarClient GetInstance() public SqlSugarClient GetInstance()
{ {

View File

@@ -12,16 +12,12 @@ namespace OrmTest.UnitTest
{ {
public void Init() public void Init()
{ {
//IsAutoCloseConnection //IsAutoCloseConnection
for (int i = 0; i < 200; i++) for (int i = 0; i < 200; i++)
{ {
var db = GetInstance(); var db = GetInstance();
var x = db.Queryable<Student>().ToList(); var x = db.Queryable<Student>().ToList();
} }
} }
public SqlSugarClient GetInstance() public SqlSugarClient GetInstance()
{ {

View File

@@ -138,6 +138,76 @@ namespace SqlSugar
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close(); if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
return count; 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) public virtual IDataReader GetDataReader(string sql, params SugarParameter[] pars)
{ {
base.SetParSize(pars); base.SetParSize(pars);

View File

@@ -14,7 +14,14 @@ namespace SqlSugar
this.Value = value; this.Value = value;
this.ParameterName = name; 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 public override System.Data.DbType DbType
{ {
get; set; get; set;
@@ -74,7 +81,5 @@ namespace SqlSugar
{ {
this.DbType = System.Data.DbType.String; this.DbType = System.Data.DbType.String;
} }
public bool IsOutput { get; set; }
} }
} }

View File

@@ -82,5 +82,10 @@ namespace SqlSugar
void BeginTran(IsolationLevel iso, string transactionName); void BeginTran(IsolationLevel iso, string transactionName);
void RollbackTran(); void RollbackTran();
void CommitTran(); void CommitTran();
SugarMessageResult<bool> UseTran(Action action);
SugarMessageResult<T> UseTran<T>(Func<T> action);
void UseStoredProcedure(Action action);
T UseStoredProcedure<T>(Func<T> action);
} }
} }

View File

@@ -85,9 +85,7 @@ namespace SqlSugar
p.Size = paramter.Size; p.Size = paramter.Size;
p.Value = paramter.Value; p.Value = paramter.Value;
p.DbType = paramter.DbType; p.DbType = paramter.DbType;
if (paramter.IsOutput) { p.Direction = paramter.Direction;
p.Direction = ParameterDirection.Output;
}
result[i] =p; result[i] =p;
++i; ++i;
} }

View File

@@ -87,8 +87,7 @@ namespace SqlSugar
p.Size = parameter.Size; p.Size = parameter.Size;
p.Value = parameter.Value; p.Value = parameter.Value;
p.DbType = parameter.DbType; p.DbType = parameter.DbType;
if (parameter.IsOutput) p.Direction = parameter.Direction;
p.Direction = ParameterDirection.Output;
result[i] = p; result[i] = p;
++i; ++i;
} }

View File

@@ -306,74 +306,5 @@ 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;
}
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
} }
} }