修复执行无返回值的存储过程问题

This commit is contained in:
falcon 2020-12-17 14:24:43 +08:00
parent 3b94b8e6ea
commit d351aec1e4
4 changed files with 38 additions and 7 deletions

View File

@ -18,5 +18,9 @@ namespace Faclon.StoredProcedureRunner.Example.Database
public IEnumerable<Sp1_Result> RunSp1(Sp1 data) { public IEnumerable<Sp1_Result> RunSp1(Sp1 data) {
return this.SpRunner.Run<Sp1,Sp1_Result>(this,data); return this.SpRunner.Run<Sp1,Sp1_Result>(this,data);
} }
public int RunSp1No(Sp1 data) {
return this.SpRunner.Execute(this,data);
}
} }
} }

View File

@ -20,6 +20,11 @@ namespace Faclon.StoredProcedureRunner.Example.Database.sp
/// 整数2 /// 整数2
/// </summary> /// </summary>
public int P2 { get; set; } = 2; public int P2 { get; set; } = 2;
/// <summary>
/// 字符串P3
/// </summary>
[FalconSPPrarmType(System.Data.SqlDbType.VarChar)]
public string P3 { get; set; } = "abcd";
} }
/// <summary> /// <summary>
/// 存储过程执行结果 /// 存储过程执行结果
@ -46,5 +51,9 @@ namespace Faclon.StoredProcedureRunner.Example.Database.sp
/// 求除法 /// 求除法
/// </summary> /// </summary>
public double Chu { get; set; } public double Chu { get; set; }
/// <summary>
/// 字符串返回值
/// </summary>
public string s { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using Faclon.StoredProcedureRunner.Example.Database; using Faclon.StoredProcedureRunner.Example.Database;
using Falcon.StoredProcedureRunner; using Falcon.StoredProcedureRunner;
@ -14,6 +15,7 @@ namespace Faclon.StoredProcedureRunner.Example
var opb = new DbContextOptionsBuilder(); var opb = new DbContextOptionsBuilder();
opb.UseSqlServer(conStr); opb.UseSqlServer(conStr);
var db = new MyDb(opb.Options,runner); var db = new MyDb(opb.Options,runner);
Console.WriteLine("测试调用存储过程,获取返回值");
var r = db.RunSp1(new Database.sp.Sp1()); var r = db.RunSp1(new Database.sp.Sp1());
Console.WriteLine("返回记录数{0},应该为2",r.Count()); Console.WriteLine("返回记录数{0},应该为2",r.Count());
var fir = r.First(); var fir = r.First();
@ -22,6 +24,11 @@ namespace Faclon.StoredProcedureRunner.Example
Console.WriteLine("Jian{0},应该为-1",fir.Jian); Console.WriteLine("Jian{0},应该为-1",fir.Jian);
Console.WriteLine("Chen{0},应该为2",fir.Chen); Console.WriteLine("Chen{0},应该为2",fir.Chen);
Console.WriteLine("Chu{0},应该为0.5",fir.Chu); Console.WriteLine("Chu{0},应该为0.5",fir.Chu);
Console.WriteLine("s{0},应该为abc1",fir.s);
Console.WriteLine("测试无返回值调用");
var r1 = db.RunSp1No(new Database.sp.Sp1());
Console.WriteLine("返回结果{0}",r1);
} }
} }
} }

View File

@ -23,13 +23,9 @@ namespace Falcon.StoredProcedureRunner
public int Execute<TPrarmType>(DbContext db,TPrarmType data) { public int Execute<TPrarmType>(DbContext db,TPrarmType data) {
var parms = getParams(typeof(TPrarmType),data).ToArray(); var parms = getParams(typeof(TPrarmType),data).ToArray();
var pName = getProcuderName<TPrarmType>(); var pName = getProcuderName<TPrarmType>();
var paramStr = getParamStr(typeof(TPrarmType),data);
#if NETSTANDARD2_1 var str = $"exec {pName} {paramStr}";
return db.Database.ExecuteSqlRaw(pName,parms); return db.Database.ExecuteSqlRaw(str,parms);
#else
return db.Database.ExecuteSqlCommand(pName,parms);
#endif
;
} }
/// <summary> /// <summary>
@ -217,5 +213,20 @@ namespace Falcon.StoredProcedureRunner
return null; return null;
} }
/// <summary>
/// 生成存储过程参数字符串
/// </summary>
/// <param name="type">参数类型</param>
/// <param name="data">参数对象</param>
/// <returns>一个参数字符串。比如@p2=@p4,@p1=@p3</returns>
private static string getParamStr(Type type,object data) {
var paras = getParams(type,data).ToArray();
var result = " ";
for(int i = 0;i < paras.Count();i++) {
result += $"{paras[i].ParameterName}={{{i}}},";
}
return result.TrimEnd(',');
}
} }
} }