[英]I get a "Specified method is not supported" error when i try to use ExcelDataReader to upload file to database
I've written a method, BulkCopy, to upload my Excel file to SQL Server database table.我编写了一个方法 BulkCopy,将我的 Excel 文件上传到 SQL Server 数据库表。 I am trying to unit test this and it fails each time with "System.NotSupportedException : Specified method is not supported".
我正在尝试对此进行单元测试,但每次都失败并显示“System.NotSupportedException:不支持指定的方法”。
If someone could have a look it would be much appreciated.如果有人可以看看,将不胜感激。
Kind regards,亲切的问候,
Emmett埃米特
public static void BulkCopy(string inputFilePath, string tableName)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var stream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
using (var bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.EnableStreaming = true;
bulkCopy.DestinationTableName = tableName;
reader.Read();
var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
foreach (var col in cols)
{
var column = cols.GetValue(0).ToString();
if (column.Trim() == "Column 1")
{
bulkCopy.ColumnMappings.Add(column, "Column 1");
}
if (column.Trim() == "Column 2")
{
bulkCopy.ColumnMappings.Add(column, "Column 2");
}
if (column.Trim() == "Column 3")
{
bulkCopy.ColumnMappings.Add(column, "Column 3");
}
//continued for column mappings...
}
bulkCopy.WriteToServer(reader);
}
Console.WriteLine("Copy data to database done (DataReader).");
}
}
I tested your code ,the issue is showed in the following code ,you reader is incorrect.我测试了您的代码,问题显示在以下代码中,您的阅读器不正确。
bulkCopy.WriteToServer(reader);
Pass the datatable into bulk , try the below code将数据表批量传递,试试下面的代码
public static void BulkCopy(string inputFilePath, string tableName)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var stream = System.IO.File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
IExcelDataReader reader;
if (inputFilePath.EndsWith(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (inputFilePath.EndsWith(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
else
throw new Exception("The file to be processed is not an Excel file");
var conf = new ExcelDataSetConfiguration
{
ConfigureDataTable = _ => new ExcelDataTableConfiguration
{
UseHeaderRow = true
}
};
var dataSet = reader.AsDataSet(conf);
// Now you can get data from each sheet by its index or its "name"
var dataTable = dataSet.Tables[0];
using (var bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.EnableStreaming = true;
bulkCopy.DestinationTableName = tableName;
reader.Read();
var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
foreach (var col in cols)
{
var column =col.ToString();
if (column.Trim() == "Column 1")
{
bulkCopy.ColumnMappings.Add(column, "Column1");
}
if (column.Trim() == "Column 2")
{
bulkCopy.ColumnMappings.Add(column, "Column2");
}
if (column.Trim() == "Column 3")
{
bulkCopy.ColumnMappings.Add(column, "Column3");
}
//continued for column mappings...
}
bulkCopy.WriteToServer(dataTable);
}
Console.WriteLine("Copy data to database done (DataReader).");
}
Pass the dataReader into bulk , change your foreach
part as shown将 dataReader 传入批量,如图所示更改
foreach
部分
for (var i = 0; i<cols.Count();i++)
{
if (cols[i].ToString().Trim() == "Column 1")
{
bulkCopy.ColumnMappings.Add(i, "Column1");
}
if (cols[i].ToString().Trim() == "Column 2")
{
bulkCopy.ColumnMappings.Add(i, "Column2");
}
if (cols[i].ToString().Trim() == "Column 3")
{
bulkCopy.ColumnMappings.Add(i, "Column3");
}
//continued for column mappings...
}
You need to change the way it's setting the column mappings.您需要更改它设置列映射的方式。 If you delete them it will work.
如果您删除它们,它将起作用。 In the example link you posted they are using
GetName
to get the columns.在您发布的示例链接中,他们使用
GetName
来获取列。 I tried that and it doesn't work for some reason.我试过了,但由于某种原因它不起作用。 I get a
GetOrdinal
error.我收到一个
GetOrdinal
错误。 That's the same error that is coming from your code: ExcelDataReader.ExcelDataReader`2.GetOrdinal(String name)
.这与来自您的代码的错误相同:
ExcelDataReader.ExcelDataReader`2.GetOrdinal(String name)
。
Not sure how pretty it is, but it works.不知道它有多漂亮,但它有效。
public static void BulkCopy(string inputFilePath, string tableName)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var stream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read);
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
using (var bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.EnableStreaming = true;
bulkCopy.DestinationTableName = tableName;
reader.Read();
var cols = Enumerable.Range(0, reader.FieldCount).Select(i => reader.GetValue(i)).ToArray();
foreach (var col in cols)
{
if (cols[i].ToString() == "Column 1")
{
bulkCopy.ColumnMappings.Add(i, "Column 1");
}
if (cols[i].ToString() == "Column 2")
{
bulkCopy.ColumnMappings.Add(i, "Column 2");
}
if (cols[i].ToString() == "Column 3")
{
bulkCopy.ColumnMappings.Add(i, "Column 3");
}
//continued for column mappings...
}
bulkCopy.WriteToServer(reader);
}
Console.WriteLine("Copy data to database done (DataReader).");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.