为返回值对象赋值失败时会引发ReturnDataSetValueException异常。

This commit is contained in:
falcon 2020-12-14 10:52:14 +08:00
parent 9b1b54c5ec
commit b031355576
2 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,22 @@
using System;
using System.Reflection;
namespace Falcon.StoredProcedureRunner
{
/// <summary>
/// 设置返回对象值时候发生异常
/// </summary>
public class ReturnDataSetValueException:Exception
{
/// <summary>
/// 实例化一个给返回数据设置值的异常
/// </summary>
/// <param name="rowId">返回数据行号</param>
/// <param name="cName">返回的列名</param>
/// <param name="pi">要设置值的属性</param>
/// <param name="value">要设置的值</param>
/// <param name="innException">内部异常</param>
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) { }
}
}

View File

@ -86,6 +86,7 @@ namespace Falcon.StoredProcedureRunner
var result = new List<object>(); var result = new List<object>();
if(!dr.CanGetColumnSchema()) if(!dr.CanGetColumnSchema())
return result; return result;
int rowId = 0;
while(dr.Read()) { while(dr.Read()) {
var item = returnType.Assembly.CreateInstance(returnType.FullName); var item = returnType.Assembly.CreateInstance(returnType.FullName);
var columnSchema = dr.GetColumnSchema(); var columnSchema = dr.GetColumnSchema();
@ -95,9 +96,14 @@ namespace Falcon.StoredProcedureRunner
var pi = getProperty(returnType,name); var pi = getProperty(returnType,name);
if(pi == null || !pi.CanWrite) if(pi == null || !pi.CanWrite)
continue; continue;
try {
pi.SetValue(item,value); pi.SetValue(item,value);
} catch(Exception ex) {
throw new ReturnDataSetValueException(rowId,name,pi,value,ex);
}
} }
result.Add(item); result.Add(item);
rowId++;
} }
connection.Close(); connection.Close();
return result; return result;