Skip to content

Commit

Permalink
IDataReader新增扩展方法ToDataSet和ToDynamics;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Sep 27, 2018
1 parent 8b606d0 commit e161cd4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
68 changes: 68 additions & 0 deletions SQLBuilder.Core/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,43 @@ public static DataTable ToDataTable<T>(this List<T> @this)
}
#endregion

#region ToDataSet
/// <summary>
/// IDataReader转换为DataSet
/// </summary>
/// <param name="this">reader数据源</param>
/// <returns>DataSet</returns>
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
/// <summary>
/// IDataReader数据转为dynamic对象
Expand Down Expand Up @@ -657,6 +694,37 @@ public static IEnumerable<dynamic> ToDynamics(this IDataReader @this)
}
}
}

/// <summary>
/// IDataReader数据转为List&lt;dynamic&gt;集合的集合
/// </summary>
/// <param name="this">IDataReader数据源</param>
/// <returns>List&lt;dynamic&gt;集合的集合</returns>
public static List<List<dynamic>> ToListDynamics(this IDataReader @this)
{
var result = new List<List<dynamic>>();
if (@this?.IsClosed == false)
{
using (@this)
{
do
{
var list = new List<dynamic>();
while (@this.Read())
{
var row = new ExpandoObject() as IDictionary<string, object>;
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
Expand Down
10 changes: 5 additions & 5 deletions SQLBuilder.Core/SQLBuilder.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>1.0.1.9</AssemblyVersion>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<Authors>张强</Authors>
<Version>1.0.1.9</Version>
<Version>1.0.2.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Expression表达式转换为SQL语句,支持SqlServer、MySQL、Oracle、SQLite、PostgreSQL</Description>
<Copyright>Copyright © 2018 , 张强 [email protected]</Copyright>
<PackageIconUrl>https://zqlovejyc.gitee.io/zqutils-js/Images/SQL.png</PackageIconUrl>
<PackageId>Zq.SQLBuilder.Core</PackageId>
<PackageReleaseNotes>1.更新Npgsql版本
<PackageReleaseNotes>1.修复OracleRepository仓储FindListAsync方法sql拼写错误

2.修复仓储Update一个重载方法BUG;</PackageReleaseNotes>
<FileVersion>1.0.1.9</FileVersion>
2.IDataReader新增扩展方法ToDataSet和ToListDynamics;</PackageReleaseNotes>
<FileVersion>1.0.2.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit e161cd4

Please sign in to comment.