From 9ba9d7b8404bb4686594874a4eb77c562a05264c Mon Sep 17 00:00:00 2001 From: zqlovejyc <943620963@qq.com> Date: Thu, 27 Sep 2018 10:45:37 +0800 Subject: [PATCH] =?UTF-8?q?IDataReader=E6=96=B0=E5=A2=9E=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E6=96=B9=E6=B3=95ToDataSet=E5=92=8CToListDynamics=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SQLBuilder/Extensions.cs | 68 +++++++++++++++++++++++++++ SQLBuilder/Properties/AssemblyInfo.cs | 4 +- SQLBuilder/SQLBuilder.csproj | 1 + 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/SQLBuilder/Extensions.cs b/SQLBuilder/Extensions.cs index c33e687..62e9d6e 100644 --- a/SQLBuilder/Extensions.cs +++ b/SQLBuilder/Extensions.cs @@ -624,6 +624,43 @@ public static DataTable ToDataTable(this List @this) } #endregion + #region ToDataSet + /// + /// IDataReader转换为DataSet + /// + /// reader数据源 + /// DataSet + public static DataSet ToDataSet(this IDataReader @this) + { + var ds = new DataSet(); + if (@this.IsClosed == false) + { + do + { + var schemaTable = @this.GetSchemaTable(); + var dt = new DataTable(); + for (var i = 0; i < schemaTable.Rows.Count; i++) + { + var row = schemaTable.Rows[i]; + dt.Columns.Add(new DataColumn((string)row["ColumnName"], (Type)row["DataType"])); + } + while (@this.Read()) + { + var dataRow = dt.NewRow(); + for (var i = 0; i < @this.FieldCount; i++) + { + dataRow[i] = @this.GetValue(i); + } + dt.Rows.Add(dataRow); + } + ds.Tables.Add(dt); + } + while (@this.NextResult()); + } + return ds; + } + #endregion + #region ToDynamic /// /// IDataReader数据转为dynamic对象 @@ -658,6 +695,37 @@ public static IEnumerable ToDynamics(this IDataReader @this) } } } + + /// + /// IDataReader数据转为List<dynamic>集合的集合 + /// + /// IDataReader数据源 + /// List<dynamic>集合的集合 + public static List> ToListDynamics(this IDataReader @this) + { + var result = new List>(); + if (@this?.IsClosed == false) + { + using (@this) + { + do + { + var list = new List(); + while (@this.Read()) + { + var row = new ExpandoObject() as IDictionary; + for (var i = 0; i < @this.FieldCount; i++) + { + row.Add(@this.GetName(i), @this.GetValue(i)); + } + list.Add(row); + } + result.Add(list); + } while (@this.NextResult()); + } + } + return result; + } #endregion #region ToDictionary diff --git a/SQLBuilder/Properties/AssemblyInfo.cs b/SQLBuilder/Properties/AssemblyInfo.cs index bdeaab5..c708bcc 100644 --- a/SQLBuilder/Properties/AssemblyInfo.cs +++ b/SQLBuilder/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.9.5")] -[assembly: AssemblyFileVersion("1.0.9.5")] +[assembly: AssemblyVersion("1.0.9.6")] +[assembly: AssemblyFileVersion("1.0.9.6")] diff --git a/SQLBuilder/SQLBuilder.csproj b/SQLBuilder/SQLBuilder.csproj index 21190a8..fe5df60 100644 --- a/SQLBuilder/SQLBuilder.csproj +++ b/SQLBuilder/SQLBuilder.csproj @@ -138,6 +138,7 @@ +