From c268970c7185596253c4a8049607cfde30789ee9 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Fri, 20 Dec 2019 21:53:44 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20IInsert.ToDataTable=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E4=B8=BA=20BulkCopy=20=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=8F=90=E4=BE=9B=E6=95=B0=E6=8D=AE=EF=BC=8C=E8=AF=A5?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=84=E7=90=86=E4=BA=86(=E8=A1=A8?= =?UTF-8?q?=E5=90=8D=E3=80=81=E5=AD=97=E6=AE=B5=E5=90=8D=E3=80=81=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=89=E6=98=A0=E5=B0=84=E5=92=8C=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E5=88=97=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 7 + FreeSql/FreeSql.xml | 131 ++++++++++++++++++ FreeSql/Interface/Curd/IInsert.cs | 10 ++ .../Internal/CommonProvider/InsertProvider.cs | 25 ++++ 4 files changed, 173 insertions(+) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index dc0203b8..d9f91124 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -110,6 +110,13 @@ 清空状态数据 + + + 根据 lambda 条件删除数据 + + + + 添加 diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index 76a0f62e..9033ba51 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -2178,6 +2178,137 @@ + + + 查询,若使用读写分离,查询【从库】条件cmdText.StartsWith("SELECT "),否则查询【主库】 + + + + + + + + + 查询,ExecuteReaderAsync(dr => {}, "select * from user where age > @age", new { age = 25 }) + + + + + + + 查询 + + + + + + + 查询,ExecuteArrayAsync("select * from user where age > @age", new { age = 25 }) + + + + + + + + 查询 + + + + + + + 查询,ExecuteDataSetAsync("select * from user where age > @age; select 2", new { age = 25 }) + + + + + + + + 查询 + + + + + + + 查询,ExecuteDataTableAsync("select * from user where age > @age", new { age = 25 }) + + + + + + + + 在【主库】执行 + + + + + + + + 在【主库】执行,ExecuteNonQueryAsync("delete from user where age > @age", new { age = 25 }) + + + + + + + + 在【主库】执行 + + + + + + + + 在【主库】执行,ExecuteScalarAsync("select 1 from user where age > @age", new { age = 25 }) + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + 执行SQL返回对象集合,QueryAsync<User>("select * from user where age > @age", new { age = 25 }) + + + + + + + + + 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new SqlParameter { ParameterName = "age", Value = 25 }) + + + + + + + + + + 执行SQL返回对象集合,Query<User>("select * from user where age > @age; select * from address", new { age = 25 }) + + + + + + 可自定义解析表达式 diff --git a/FreeSql/Interface/Curd/IInsert.cs b/FreeSql/Interface/Curd/IInsert.cs index 74a77ca5..77ddd620 100644 --- a/FreeSql/Interface/Curd/IInsert.cs +++ b/FreeSql/Interface/Curd/IInsert.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Data.Common; using System.Linq.Expressions; using System.Threading.Tasks; @@ -115,6 +116,15 @@ namespace FreeSql /// List ExecuteInserted(); + /// + /// 返回 DataTable 以便做 BulkCopy 数据做准备 + /// 此方法会处理: + /// 类型、表名、字段名映射 + /// IgnoreColumns、InsertColumns + /// + /// + DataTable ToDataTable(); + #if net40 #else Task ExecuteAffrowsAsync(); diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs index a606b4d3..d634ad30 100644 --- a/FreeSql/Internal/CommonProvider/InsertProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs @@ -464,6 +464,31 @@ namespace FreeSql.Internal.CommonProvider _params = specialParams.ToArray(); return sb.ToString(); } + + public DataTable ToDataTable() + { + var dt = new DataTable(); + dt.TableName = TableRuleInvoke(); + foreach (var col in _table.ColumnsByPosition) + { + if (col.Attribute.IsIdentity && _insertIdentity == false) continue; + if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue; + dt.Columns.Add(col.Attribute.Name, col.Attribute.MapType); + } + foreach (var d in _source) + { + var row = new object[dt.Columns.Count]; + var rowIndex = 0; + foreach (var col in _table.ColumnsByPosition) + { + if (col.Attribute.IsIdentity && _insertIdentity == false) continue; + if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue; + row[rowIndex++] = col.GetMapValue(d); + } + dt.Rows.Add(row); + } + return dt; + } } }