增加一个通过DataSet做过度的方法,仅供测试

This commit is contained in:
falcon 2021-12-15 10:41:05 +08:00
parent f03604fa0d
commit 89f3a406c2

View File

@ -1,5 +1,6 @@
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using Oracle.ManagedDataAccess.Client; using Oracle.ManagedDataAccess.Client;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -44,17 +45,31 @@ namespace Falcon.StoredProcedureRunner
/// <param name="data">存储过程参数</param> /// <param name="data">存储过程参数</param>
/// <returns>查询结果枚举</returns> /// <returns>查询结果枚举</returns>
public IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data) public IEnumerable<object> Run(DbContext db, Type prarmType, Type returnType, object data)
{
return RunByReader(db, prarmType, returnType, data);
}
/// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary>
/// <param name="db">数据库上下文</param>
/// <param name="prarmType">参数类型</param>
/// <param name="returnType">返回值类型</param>
/// <param name="data">存储过程参数</param>
/// <returns>查询结果枚举</returns>
public IEnumerable<object> RunByReader(DbContext db, Type prarmType, Type returnType, object data)
{ {
var pm = getProcuderName(prarmType); var pm = getProcuderName(prarmType);
var paras = getParams(db, prarmType, data).ToArray(); var paras = getParams(db, prarmType, data).ToArray();
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 = CommandType.StoredProcedure; cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(paras); cmd.Parameters.AddRange(paras);
connection.Open(); connection.Open();
var dr = cmd.ExecuteReader(); using var dr = cmd.ExecuteReader();
var columnSchema = dr.GetColumnSchema();
var result = new List<object>(); var result = new List<object>();
if (!dr.CanGetColumnSchema()) if (!dr.CanGetColumnSchema())
return result; return result;
@ -62,7 +77,6 @@ namespace Falcon.StoredProcedureRunner
while (dr.Read()) while (dr.Read())
{ {
var item = returnType.Assembly.CreateInstance(returnType.FullName); var item = returnType.Assembly.CreateInstance(returnType.FullName);
var columnSchema = dr.GetColumnSchema();
for (var i = 0; i < columnSchema.Count; i++) for (var i = 0; i < columnSchema.Count; i++)
{ {
var name = dr.GetName(i); var name = dr.GetName(i);
@ -85,7 +99,74 @@ namespace Falcon.StoredProcedureRunner
connection.Close(); connection.Close();
return result; return result;
} }
/// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary>
/// <param name="db">数据库上下文</param>
/// <param name="prarmType">参数类型</param>
/// <param name="returnType">返回值类型</param>
/// <param name="data">存储过程参数</param>
/// <returns>查询结果枚举</returns>
public IEnumerable<object> RunByDbset(DbContext db, Type prarmType, Type returnType, object data)
{
var result = new List<object>();
var pm = getProcuderName(prarmType);
var paras = getParams(db, prarmType, data).ToArray();
DbConnection connection = db.Database.GetDbConnection();
DataSet ds = new DataSet();
if (db.Database.IsOracle())
{
var conn = db.Database.GetDbConnection() as OracleConnection;
using var cmd = new OracleCommand(pm, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(paras);
var da = new OracleDataAdapter(cmd);
da.Fill(ds);
} }
else if (db.Database.IsSqlServer())
{
var oracleConn = db.Database.GetDbConnection() as SqlConnection;
using var oracleCmd = new SqlCommand(pm, oracleConn);
oracleCmd.CommandType = CommandType.StoredProcedure;
oracleCmd.Parameters.AddRange(paras);
var da = new SqlDataAdapter(oracleCmd);
da.Fill(ds);
}
else
{
throw new DatabaseNotSupportedException();
}
var rows = ds.Tables[0].Rows;
var cols = ds.Tables[0].Columns;
for (int i = 0; i < rows.Count; i++)
{
var item = returnType.Assembly.CreateInstance(returnType.FullName);
var row = rows[i];
for (int y = 0; y < cols.Count; y++)
{
var name = cols[y].ColumnName;
var val = row[y] is DBNull ? null : row[y];
var pi = getProperty(returnType, name);
if (pi == null || !pi.CanWrite)
continue;
try
{
pi.SetValue(item, val);
}
catch (Exception ex)
{
throw new ReturnDataSetValueException(i, name, pi, val, ex);
}
}
result.Add(item);
}
connection.Close();
return result;
}
/// <summary> /// <summary>
/// 存在sql注入风险执行Sql语句并将数据库返回结果以json数据对象返回。 /// 存在sql注入风险执行Sql语句并将数据库返回结果以json数据对象返回。