Skip to content

Commit

Permalink
IDataReader新增扩展方法ToDataSet和ToListDynamics;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Sep 27, 2018
1 parent bacde86 commit 9ba9d7b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
68 changes: 68 additions & 0 deletions SQLBuilder/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,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 @@ -658,6 +695,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
4 changes: 2 additions & 2 deletions SQLBuilder/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
1 change: 1 addition & 0 deletions SQLBuilder/SQLBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<None Include="Nuget\Zq.SQLBuilder.1.0.9.2.nupkg" />
<None Include="Nuget\Zq.SQLBuilder.1.0.9.3.nupkg" />
<None Include="Nuget\Zq.SQLBuilder.1.0.9.5.nupkg" />
<None Include="Nuget\Zq.SQLBuilder.1.0.9.6.nupkg" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down

0 comments on commit 9ba9d7b

Please sign in to comment.