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;