mirror of
https://github.com/FalconWu2017/Falcon.StoredProcedureRunner.git
synced 2024-11-23 05:29:37 +08:00
修复执行无返回值的存储过程问题
This commit is contained in:
parent
3b94b8e6ea
commit
d351aec1e4
|
@ -18,5 +18,9 @@ namespace Faclon.StoredProcedureRunner.Example.Database
|
|||
public IEnumerable<Sp1_Result> RunSp1(Sp1 data) {
|
||||
return this.SpRunner.Run<Sp1,Sp1_Result>(this,data);
|
||||
}
|
||||
|
||||
public int RunSp1No(Sp1 data) {
|
||||
return this.SpRunner.Execute(this,data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,11 @@ namespace Faclon.StoredProcedureRunner.Example.Database.sp
|
|||
/// 整数2
|
||||
/// </summary>
|
||||
public int P2 { get; set; } = 2;
|
||||
/// <summary>
|
||||
/// 字符串P3
|
||||
/// </summary>
|
||||
[FalconSPPrarmType(System.Data.SqlDbType.VarChar)]
|
||||
public string P3 { get; set; } = "abcd";
|
||||
}
|
||||
/// <summary>
|
||||
/// 存储过程执行结果
|
||||
|
@ -46,5 +51,9 @@ namespace Faclon.StoredProcedureRunner.Example.Database.sp
|
|||
/// 求除法
|
||||
/// </summary>
|
||||
public double Chu { get; set; }
|
||||
/// <summary>
|
||||
/// 字符串返回值
|
||||
/// </summary>
|
||||
public string s { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using Faclon.StoredProcedureRunner.Example.Database;
|
||||
using Falcon.StoredProcedureRunner;
|
||||
|
@ -14,6 +15,7 @@ namespace Faclon.StoredProcedureRunner.Example
|
|||
var opb = new DbContextOptionsBuilder();
|
||||
opb.UseSqlServer(conStr);
|
||||
var db = new MyDb(opb.Options,runner);
|
||||
Console.WriteLine("测试调用存储过程,获取返回值");
|
||||
var r = db.RunSp1(new Database.sp.Sp1());
|
||||
Console.WriteLine("返回记录数{0},应该为2",r.Count());
|
||||
var fir = r.First();
|
||||
|
@ -22,6 +24,11 @@ namespace Faclon.StoredProcedureRunner.Example
|
|||
Console.WriteLine("Jian{0},应该为-1",fir.Jian);
|
||||
Console.WriteLine("Chen{0},应该为2",fir.Chen);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,13 +23,9 @@ namespace Falcon.StoredProcedureRunner
|
|||
public int Execute<TPrarmType>(DbContext db,TPrarmType data) {
|
||||
var parms = getParams(typeof(TPrarmType),data).ToArray();
|
||||
var pName = getProcuderName<TPrarmType>();
|
||||
|
||||
#if NETSTANDARD2_1
|
||||
return db.Database.ExecuteSqlRaw(pName,parms);
|
||||
#else
|
||||
return db.Database.ExecuteSqlCommand(pName,parms);
|
||||
#endif
|
||||
;
|
||||
var paramStr = getParamStr(typeof(TPrarmType),data);
|
||||
var str = $"exec {pName} {paramStr}";
|
||||
return db.Database.ExecuteSqlRaw(str,parms);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -217,5 +213,20 @@ namespace Falcon.StoredProcedureRunner
|
|||
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(',');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user