From b03135557641f3f4c4e79cab1be372db63646d72 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Mon, 14 Dec 2020 10:52:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E8=BF=94=E5=9B=9E=E5=80=BC=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E8=B5=8B=E5=80=BC=E5=A4=B1=E8=B4=A5=E6=97=B6=E4=BC=9A?= =?UTF-8?q?=E5=BC=95=E5=8F=91ReturnDataSetValueException=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReturnDataSetValueException.cs | 22 +++++++++++++++++++ src/Falcon.StoredProcedureRunner/Runner.cs | 8 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/Falcon.StoredProcedureRunner/ReturnDataSetValueException.cs diff --git a/src/Falcon.StoredProcedureRunner/ReturnDataSetValueException.cs b/src/Falcon.StoredProcedureRunner/ReturnDataSetValueException.cs new file mode 100644 index 0000000..10b4b7c --- /dev/null +++ b/src/Falcon.StoredProcedureRunner/ReturnDataSetValueException.cs @@ -0,0 +1,22 @@ +using System; +using System.Reflection; + +namespace Falcon.StoredProcedureRunner +{ + /// + /// 设置返回对象值时候发生异常 + /// + public class ReturnDataSetValueException:Exception + { + /// + /// 实例化一个给返回数据设置值的异常 + /// + /// 返回数据行号 + /// 返回的列名 + /// 要设置值的属性 + /// 要设置的值 + /// 内部异常 + public ReturnDataSetValueException(int rowId,string cName, PropertyInfo pi,object value,Exception innException) + : base($"存储过程返回第[{rowId}]行[{cName}]列数据值为({value.GetType().FullName})[{value}]无法赋给属性({pi.PropertyType.FullName})[{pi.Name}]。",innException) { } + } +} diff --git a/src/Falcon.StoredProcedureRunner/Runner.cs b/src/Falcon.StoredProcedureRunner/Runner.cs index 9bad42f..91ca66b 100644 --- a/src/Falcon.StoredProcedureRunner/Runner.cs +++ b/src/Falcon.StoredProcedureRunner/Runner.cs @@ -86,6 +86,7 @@ namespace Falcon.StoredProcedureRunner var result = new List(); if(!dr.CanGetColumnSchema()) return result; + int rowId = 0; while(dr.Read()) { var item = returnType.Assembly.CreateInstance(returnType.FullName); var columnSchema = dr.GetColumnSchema(); @@ -95,9 +96,14 @@ namespace Falcon.StoredProcedureRunner var pi = getProperty(returnType,name); if(pi == null || !pi.CanWrite) continue; - pi.SetValue(item,value); + try { + pi.SetValue(item,value); + } catch(Exception ex) { + throw new ReturnDataSetValueException(rowId,name,pi,value,ex); + } } result.Add(item); + rowId++; } connection.Close(); return result;