- [Bug] Fix SaveAs by datareader error "Invalid attempt to call FieldCount when reader is closed" #230
This commit is contained in:
wei 2021-05-13 11:41:07 +08:00
parent a72b469cdb
commit 1463c16ba0
6 changed files with 72 additions and 6 deletions

View File

@ -16,6 +16,9 @@
---
### 0.14.6
- [Bug] Fix SaveAs by datareader error "Invalid attempt to call FieldCount when reader is closed" #230
### 0.14.5
- [Breaking Change] Rename OpenXmlConfiguration FillMergedCells

View File

@ -17,6 +17,9 @@
---
### 0.14.6
- [Bug] 修正 SaveAs by datareader 错误 "Invalid attempt to call FieldCount when reader is closed" #230
### 0.14.5
- [Breaking Change] 更换 OpenXmlConfiguration FillMergedCells 名称

View File

@ -17,6 +17,9 @@
---
### 0.14.6
- [Bug] 修正 SaveAs by datareader 錯誤 "Invalid attempt to call FieldCount when reader is closed" #230
### 0.14.5
- [Breaking Change] 更換 OpenXmlConfiguration FillMergedCells 名稱

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
<Version>0.14.5</Version>
<Version>0.14.6</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniExcel</AssemblyName>

View File

@ -44,7 +44,12 @@ namespace MiniExcelLibs.OpenXml
Type genericType = null;
//DapperRow
if (value is IEnumerable)
if (value is IDataReader)
{
GenerateSheetByIDataReader(writer, archive, value as IDataReader, printHeader);
}
else if (value is IEnumerable)
{
var values = value as IEnumerable;
@ -165,10 +170,6 @@ namespace MiniExcelLibs.OpenXml
{
GenerateSheetByDataTable(writer, archive, value as DataTable, printHeader);
}
else if (value is IDataReader)
{
GenerateSheetByIDataReader(writer, archive, value as IDataReader, printHeader);
}
else
{
throw new NotImplementedException($"Type {type.Name} & genericType {genericType.Name} not Implemented. please issue for me.");

View File

@ -15,6 +15,8 @@ using System.Data;
using System.Data.SQLite;
using Dapper;
using MiniExcelLibs.OpenXml;
using System.Data.SqlClient;
using System.Data.Common;
namespace MiniExcelLibs.Tests
{
@ -26,6 +28,60 @@ namespace MiniExcelLibs.Tests
this.output = output;
}
/// <summary>
/// SaveAs By Reader Closed error : 'Error! Invalid attempt to call FieldCount when reader is closed' #230
/// https://github.com/shps951023/MiniExcel/issues/230
/// </summary>
[Fact]
public void Issue230()
{
var conn = Db.GetConnection("Data Source=:memory:");
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandText = "select 1 id union all select 2";
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
var result = $"{reader.GetName(i)} , {reader.GetValue(i)}";
output.WriteLine(result);
}
}
}
conn = Db.GetConnection("Data Source=:memory:");
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select 1 id union all select 2";
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
var result = $"{reader.GetName(i)} , {reader.GetValue(i)}";
output.WriteLine(result);
}
}
}
conn = Db.GetConnection("Data Source=:memory:");
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select 1 id union all select 2";
using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
var path = PathHelper.GetNewTemplateFilePath();
MiniExcel.SaveAs(path, reader, printHeader: true);
var rows = MiniExcel.Query(path,true).ToList();
Assert.Equal(1, rows[0].id);
Assert.Equal(2, rows[1].id);
}
}
/// <summary>
/// v0.14.3 QueryAsDataTable error "Cannot set Column to be null" #229
/// https://github.com/shps951023/MiniExcel/issues/229