mirror of
https://github.com/FalconWu2017/Falcon.StoredProcedureRunner.git
synced 2024-11-23 13:39:38 +08:00
定义返回值类型异常和返回值转换异常
This commit is contained in:
parent
efc1fcd5d0
commit
ee43f42364
17
src/Falcon.StoredProcedureRunner/ReturnTypeCastException.cs
Normal file
17
src/Falcon.StoredProcedureRunner/ReturnTypeCastException.cs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 返回类型转换异常
|
||||||
|
/// </summary>
|
||||||
|
public class ReturnTypeCastException:Exception
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 构造一个返回类型转换异常。
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ex">类型转换异常</param>
|
||||||
|
public ReturnTypeCastException(Exception ex)
|
||||||
|
: base("将存储过程返回结果转换为指定返回类型时发生错误!无法转换",ex) { }
|
||||||
|
}
|
||||||
|
}
|
16
src/Falcon.StoredProcedureRunner/ReturnTypeException.cs
Normal file
16
src/Falcon.StoredProcedureRunner/ReturnTypeException.cs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.StoredProcedureRunner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 返回类型定义异常
|
||||||
|
/// </summary>
|
||||||
|
public class ReturnTypeException:Exception
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 构造一个返回类型错误异常
|
||||||
|
/// </summary>
|
||||||
|
public ReturnTypeException() : base("必须在参数类型上设置ReturnTypeAttribute或者通过合适重载明确指定返回数据类型。") {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,6 @@ using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.Data.SqlClient;
|
using Microsoft.Data.SqlClient;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
@ -44,7 +42,7 @@ namespace Falcon.StoredProcedureRunner
|
||||||
public IEnumerable<object> Run<TPrarmType>(DbContext db,TPrarmType data) {
|
public IEnumerable<object> Run<TPrarmType>(DbContext db,TPrarmType data) {
|
||||||
var rType = getRequtnType(typeof(TPrarmType));
|
var rType = getRequtnType(typeof(TPrarmType));
|
||||||
if(rType == null) {
|
if(rType == null) {
|
||||||
throw new Exception("必须在参数类型上设置ReturnTypeAttribute");
|
throw new ReturnTypeException();
|
||||||
}
|
}
|
||||||
return Run(db,typeof(TPrarmType),rType,data);
|
return Run(db,typeof(TPrarmType),rType,data);
|
||||||
}
|
}
|
||||||
|
@ -58,7 +56,13 @@ namespace Falcon.StoredProcedureRunner
|
||||||
/// <param name="data">参数数据</param>
|
/// <param name="data">参数数据</param>
|
||||||
/// <returns>查询结果枚举</returns>
|
/// <returns>查询结果枚举</returns>
|
||||||
public IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(DbContext db,TPrarmType data) where TReturnType : class, new() {
|
public IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(DbContext db,TPrarmType data) where TReturnType : class, new() {
|
||||||
|
try {
|
||||||
return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
|
return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
|
||||||
|
} catch(InvalidCastException ice) {
|
||||||
|
throw new ReturnTypeCastException(ice);
|
||||||
|
} catch(Exception ex) {
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -75,7 +79,7 @@ namespace Falcon.StoredProcedureRunner
|
||||||
var connection = db.Database.GetDbConnection();
|
var connection = db.Database.GetDbConnection();
|
||||||
using(var cmd = connection.CreateCommand()) {
|
using(var cmd = connection.CreateCommand()) {
|
||||||
cmd.CommandText = pm;
|
cmd.CommandText = pm;
|
||||||
cmd.CommandType = System.Data.CommandType.StoredProcedure;
|
cmd.CommandType = CommandType.StoredProcedure;
|
||||||
cmd.Parameters.AddRange(paras);
|
cmd.Parameters.AddRange(paras);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
var dr = cmd.ExecuteReader();
|
var dr = cmd.ExecuteReader();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user