using Microsoft.EntityFrameworkCore; using Oracle.ManagedDataAccess.Client; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using Znyc.Recruitment.Admin.Commons.IDbContext; namespace Znyc.Recruitment.Admin.Commons.DbContextCore { /// /// Oracle上下文 /// public class OracleDbContext : BaseDbContext, IOracleDbContext { /// /// /// /// /// /// public override DataTable GetDataTable(string sql, int cmdTimeout = 30, params DbParameter[] parameters) { return GetDataTables(sql, cmdTimeout, parameters).FirstOrDefault(); } /// /// /// /// /// /// public override List GetDataTables(string sql, int cmdTimeout = 30, params DbParameter[] parameters) { List dts = new List(); //TODO: connection 不能dispose 或者 用using,否则下次获取connection会报错提示“the connectionstring property has not been initialized。” DbConnection connection = Database.GetDbConnection(); if (connection.State != ConnectionState.Open) { connection.Open(); } using (OracleCommand cmd = new OracleCommand(sql, (OracleConnection)connection)) { cmd.CommandTimeout = cmdTimeout; if (parameters != null && parameters.Length > 0) { cmd.Parameters.AddRange(parameters); } using (OracleDataAdapter da = new OracleDataAdapter(cmd)) { using (DataSet ds = new DataSet()) { da.Fill(ds); foreach (DataTable table in ds.Tables) { dts.Add(table); } } } } connection.Close(); return dts; } } }