From ec7fb113a9585a0b8d7636e01a29d73dab8d79f3 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 15 Jul 2024 13:49:54 +0800 Subject: [PATCH] Update AccessCore --- .../Access/AccessProvider.cs | 4 +- .../SqlSugar.AccessCore/OleDbDataAdapter.cs | 147 ++++++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSugar.AccessCore/OleDbDataAdapter.cs diff --git a/Src/Asp.NetCore2/SqlSugar.AccessCore/Access/AccessProvider.cs b/Src/Asp.NetCore2/SqlSugar.AccessCore/Access/AccessProvider.cs index 6ef87df39..9e5f86600 100644 --- a/Src/Asp.NetCore2/SqlSugar.AccessCore/Access/AccessProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar.AccessCore/Access/AccessProvider.cs @@ -58,7 +58,7 @@ namespace SqlSugar.Access } public override IDataAdapter GetAdapter() { - return new OleDbDataAdapter(); + return new OleDbDataAdapterNew(); } public override DbCommand GetCommand(string sql, SugarParameter[] parameters) { @@ -84,7 +84,7 @@ namespace SqlSugar.Access } public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command) { - ((OleDbDataAdapter)dataAdapter).SelectCommand = (OleDbCommand)command; + ((OleDbDataAdapterNew)dataAdapter).SelectCommand = (OleDbCommand)command; } /// /// if mysql return MySqlParameter[] pars diff --git a/Src/Asp.NetCore2/SqlSugar.AccessCore/OleDbDataAdapter.cs b/Src/Asp.NetCore2/SqlSugar.AccessCore/OleDbDataAdapter.cs new file mode 100644 index 000000000..499d531e2 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar.AccessCore/OleDbDataAdapter.cs @@ -0,0 +1,147 @@ +using System; +using System.Collections.Generic; +using System.Data.OleDb; +using System.Data; +using System.Text; + +namespace SqlSugar +{ + + + /// + /// 数据填充器 + /// + public class OleDbDataAdapterNew : IDataAdapter + { + private OleDbCommand command; + private string sql; + private OleDbConnection _sqlConnection; + + /// + /// SqlDataAdapter + /// + /// + public OleDbDataAdapterNew(OleDbCommand command) + { + this.command = command; + } + + public OleDbDataAdapterNew() + { + + } + + /// + /// SqlDataAdapter + /// + /// + /// + public OleDbDataAdapterNew(string sql, OleDbConnection _sqlConnection) + { + this.sql = sql; + this._sqlConnection = _sqlConnection; + } + + /// + /// SelectCommand + /// + public OleDbCommand SelectCommand + { + get + { + if (this.command == null) + { + this.command = new OleDbCommand(this.sql, this._sqlConnection); + } + return this.command; + } + set + { + this.command = value; + } + } + + /// + /// Fill + /// + /// + public void Fill(DataTable dt) + { + if (dt == null) + { + dt = new DataTable(); + } + var columns = dt.Columns; + var rows = dt.Rows; + using (OleDbDataReader dr = command.ExecuteReader()) + { + for (int i = 0; i < dr.FieldCount; i++) + { + string name = dr.GetName(i).Trim(); + if (!columns.Contains(name)) + columns.Add(new DataColumn(name, dr.GetFieldType(i))); + else + { + columns.Add(new DataColumn(name + i, dr.GetFieldType(i))); + } + } + + while (dr.Read()) + { + DataRow daRow = dt.NewRow(); + for (int i = 0; i < columns.Count; i++) + { + daRow[columns[i].ColumnName] = dr.GetValue(i); + } + dt.Rows.Add(daRow); + } + } + + dt.AcceptChanges(); + } + + /// + /// Fill + /// + /// + public void Fill(DataSet ds) + { + if (ds == null) + { + ds = new DataSet(); + } + using (OleDbDataReader dr = command.ExecuteReader()) + { + do + { + var dt = new DataTable(); + var columns = dt.Columns; + var rows = dt.Rows; + for (int i = 0; i < dr.FieldCount; i++) + { + string name = dr.GetName(i).Trim(); + if (!columns.Contains(name)) + columns.Add(new DataColumn(name, dr.GetFieldType(i))); + else + { + columns.Add(new DataColumn(name + i, dr.GetFieldType(i))); + } + + } + + while (dr.Read()) + { + DataRow daRow = dt.NewRow(); + for (int i = 0; i < columns.Count; i++) + { + daRow[columns[i].ColumnName] = dr.GetValue(i); + } + dt.Rows.Add(daRow); + } + dt.AcceptChanges(); + ds.Tables.Add(dt); + } while (dr.NextResult()); + } + } + } +}