From d351aec1e45f6b4e924eb2c4f9f6684cdd672542 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Thu, 17 Dec 2020 14:24:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8C=E6=97=A0?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E7=9A=84=E5=AD=98=E5=82=A8=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Database/MyDb.cs | 4 +++ .../Database/sp/Sp1.cs | 9 +++++++ .../Program.cs | 7 ++++++ src/Falcon.StoredProcedureRunner/Runner.cs | 25 +++++++++++++------ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/Faclon.StoredProcedureRunner.Example/Database/MyDb.cs b/src/Faclon.StoredProcedureRunner.Example/Database/MyDb.cs index 9d7b5f9..3188ae5 100644 --- a/src/Faclon.StoredProcedureRunner.Example/Database/MyDb.cs +++ b/src/Faclon.StoredProcedureRunner.Example/Database/MyDb.cs @@ -18,5 +18,9 @@ namespace Faclon.StoredProcedureRunner.Example.Database public IEnumerable RunSp1(Sp1 data) { return this.SpRunner.Run(this,data); } + + public int RunSp1No(Sp1 data) { + return this.SpRunner.Execute(this,data); + } } } diff --git a/src/Faclon.StoredProcedureRunner.Example/Database/sp/Sp1.cs b/src/Faclon.StoredProcedureRunner.Example/Database/sp/Sp1.cs index a205fcd..4910096 100644 --- a/src/Faclon.StoredProcedureRunner.Example/Database/sp/Sp1.cs +++ b/src/Faclon.StoredProcedureRunner.Example/Database/sp/Sp1.cs @@ -20,6 +20,11 @@ namespace Faclon.StoredProcedureRunner.Example.Database.sp /// 整数2 /// public int P2 { get; set; } = 2; + /// + /// 字符串P3 + /// + [FalconSPPrarmType(System.Data.SqlDbType.VarChar)] + public string P3 { get; set; } = "abcd"; } /// /// 存储过程执行结果 @@ -46,5 +51,9 @@ namespace Faclon.StoredProcedureRunner.Example.Database.sp /// 求除法 /// public double Chu { get; set; } + /// + /// 字符串返回值 + /// + public string s { get; set; } } } diff --git a/src/Faclon.StoredProcedureRunner.Example/Program.cs b/src/Faclon.StoredProcedureRunner.Example/Program.cs index 1536fb7..020ac1f 100644 --- a/src/Faclon.StoredProcedureRunner.Example/Program.cs +++ b/src/Faclon.StoredProcedureRunner.Example/Program.cs @@ -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); } } } diff --git a/src/Falcon.StoredProcedureRunner/Runner.cs b/src/Falcon.StoredProcedureRunner/Runner.cs index 91ca66b..4f226bf 100644 --- a/src/Falcon.StoredProcedureRunner/Runner.cs +++ b/src/Falcon.StoredProcedureRunner/Runner.cs @@ -23,13 +23,9 @@ namespace Falcon.StoredProcedureRunner public int Execute(DbContext db,TPrarmType data) { var parms = getParams(typeof(TPrarmType),data).ToArray(); var pName = getProcuderName(); - -#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); } /// @@ -217,5 +213,20 @@ namespace Falcon.StoredProcedureRunner return null; } + /// + /// 生成存储过程参数字符串 + /// + /// 参数类型 + /// 参数对象 + /// 一个参数字符串。比如@p2=@p4,@p1=@p3 + 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(','); + } + } }