From 89f3a406c258703831501ef0d95eae0e33770f3f Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 15 Dec 2021 10:41:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E9=80=9A?= =?UTF-8?q?=E8=BF=87DataSet=E5=81=9A=E8=BF=87=E5=BA=A6=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E4=BB=85=E4=BE=9B=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Falcon.StoredProcedureRunner/Runner.cs | 147 ++++++++++++++++----- 1 file changed, 114 insertions(+), 33 deletions(-) diff --git a/src/Falcon.StoredProcedureRunner/Runner.cs b/src/Falcon.StoredProcedureRunner/Runner.cs index 580ab97..14d3b4f 100644 --- a/src/Falcon.StoredProcedureRunner/Runner.cs +++ b/src/Falcon.StoredProcedureRunner/Runner.cs @@ -1,5 +1,6 @@ using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json.Linq; using Oracle.ManagedDataAccess.Client; using System; using System.Collections.Generic; @@ -44,49 +45,129 @@ namespace Falcon.StoredProcedureRunner /// 存储过程参数 /// 查询结果枚举 public IEnumerable Run(DbContext db, Type prarmType, Type returnType, object data) + { + return RunByReader(db, prarmType, returnType, data); + } + + + /// + /// 通过数据库上下文执行存储过程,并返回查询结果 + /// + /// 数据库上下文 + /// 参数类型 + /// 返回值类型 + /// 存储过程参数 + /// 查询结果枚举 + public IEnumerable RunByReader(DbContext db, Type prarmType, Type returnType, object data) { var pm = getProcuderName(prarmType); var paras = getParams(db, prarmType, data).ToArray(); var connection = db.Database.GetDbConnection(); - using (var cmd = connection.CreateCommand()) + using var cmd = connection.CreateCommand(); + cmd.CommandText = pm; + cmd.CommandType = CommandType.StoredProcedure; + cmd.Parameters.AddRange(paras); + connection.Open(); + using var dr = cmd.ExecuteReader(); + var columnSchema = dr.GetColumnSchema(); + var result = new List(); + if (!dr.CanGetColumnSchema()) + return result; + int rowId = 0; + while (dr.Read()) { - cmd.CommandText = pm; + var item = returnType.Assembly.CreateInstance(returnType.FullName); + for (var i = 0; i < columnSchema.Count; i++) + { + var name = dr.GetName(i); + var value = dr.IsDBNull(i) ? null : dr.GetValue(i); + var pi = getProperty(returnType, name); + if (pi == null || !pi.CanWrite) + continue; + try + { + pi.SetValue(item, value); + } + catch (Exception ex) + { + throw new ReturnDataSetValueException(rowId, name, pi, value, ex); + } + } + result.Add(item); + rowId++; + } + connection.Close(); + return result; + } + + /// + /// 通过数据库上下文执行存储过程,并返回查询结果 + /// + /// 数据库上下文 + /// 参数类型 + /// 返回值类型 + /// 存储过程参数 + /// 查询结果枚举 + public IEnumerable RunByDbset(DbContext db, Type prarmType, Type returnType, object data) + { + var result = new List(); + 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); - connection.Open(); - var dr = cmd.ExecuteReader(); - 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(); - for (var i = 0; i < columnSchema.Count; i++) - { - var name = dr.GetName(i); - var value = dr.IsDBNull(i) ? null : dr.GetValue(i); - var pi = getProperty(returnType, name); - if (pi == null || !pi.CanWrite) - continue; - try - { - pi.SetValue(item, value); - } - catch (Exception ex) - { - throw new ReturnDataSetValueException(rowId, name, pi, value, ex); - } - } - result.Add(item); - rowId++; - } - connection.Close(); - return result; + 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; } + + + /// /// (存在sql注入风险)执行Sql语句,并将数据库返回结果以json数据对象返回。 ///