From ee43f4236473bf447edbecdc071e67156dad2325 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Sat, 12 Dec 2020 10:35:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=BC=82=E5=B8=B8=E5=92=8C=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E8=BD=AC=E6=8D=A2=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReturnTypeCastException.cs | 17 +++++++++++++++++ .../ReturnTypeException.cs | 16 ++++++++++++++++ src/Falcon.StoredProcedureRunner/Runner.cs | 14 +++++++++----- 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/Falcon.StoredProcedureRunner/ReturnTypeCastException.cs create mode 100644 src/Falcon.StoredProcedureRunner/ReturnTypeException.cs diff --git a/src/Falcon.StoredProcedureRunner/ReturnTypeCastException.cs b/src/Falcon.StoredProcedureRunner/ReturnTypeCastException.cs new file mode 100644 index 0000000..440b76f --- /dev/null +++ b/src/Falcon.StoredProcedureRunner/ReturnTypeCastException.cs @@ -0,0 +1,17 @@ +using System; + +namespace Falcon.StoredProcedureRunner +{ + /// + /// 返回类型转换异常 + /// + public class ReturnTypeCastException:Exception + { + /// + /// 构造一个返回类型转换异常。 + /// + /// 类型转换异常 + public ReturnTypeCastException(Exception ex) + : base("将存储过程返回结果转换为指定返回类型时发生错误!无法转换",ex) { } + } +} diff --git a/src/Falcon.StoredProcedureRunner/ReturnTypeException.cs b/src/Falcon.StoredProcedureRunner/ReturnTypeException.cs new file mode 100644 index 0000000..3531efa --- /dev/null +++ b/src/Falcon.StoredProcedureRunner/ReturnTypeException.cs @@ -0,0 +1,16 @@ +using System; + +namespace Falcon.StoredProcedureRunner +{ + /// + /// 返回类型定义异常 + /// + public class ReturnTypeException:Exception + { + /// + /// 构造一个返回类型错误异常 + /// + public ReturnTypeException() : base("必须在参数类型上设置ReturnTypeAttribute或者通过合适重载明确指定返回数据类型。") { + } + } +} diff --git a/src/Falcon.StoredProcedureRunner/Runner.cs b/src/Falcon.StoredProcedureRunner/Runner.cs index 55b150a..704b6dc 100644 --- a/src/Falcon.StoredProcedureRunner/Runner.cs +++ b/src/Falcon.StoredProcedureRunner/Runner.cs @@ -4,8 +4,6 @@ using System.Data; using System.Data.Common; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; -using System.Text; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; @@ -44,7 +42,7 @@ namespace Falcon.StoredProcedureRunner public IEnumerable Run(DbContext db,TPrarmType data) { var rType = getRequtnType(typeof(TPrarmType)); if(rType == null) { - throw new Exception("必须在参数类型上设置ReturnTypeAttribute"); + throw new ReturnTypeException(); } return Run(db,typeof(TPrarmType),rType,data); } @@ -58,7 +56,13 @@ namespace Falcon.StoredProcedureRunner /// 参数数据 /// 查询结果枚举 public IEnumerable Run(DbContext db,TPrarmType data) where TReturnType : class, new() { - return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast(); + try { + return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast(); + } catch(InvalidCastException ice) { + throw new ReturnTypeCastException(ice); + } catch(Exception ex) { + throw ex; + } } /// @@ -75,7 +79,7 @@ namespace Falcon.StoredProcedureRunner var connection = db.Database.GetDbConnection(); using(var cmd = connection.CreateCommand()) { cmd.CommandText = pm; - cmd.CommandType = System.Data.CommandType.StoredProcedure; + cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(paras); connection.Open(); var dr = cmd.ExecuteReader();