简体   繁体   English

FastMember:指定的参数超出有效值范围。 参数名称:名称

[英]FastMember: Specified argument was out of the range of valid values. Parameter name: name

I am attempting to use CsvHelper to import a CSV file and write it to my SQL database. 我正在尝试使用CsvHelper导入CSV文件并将其写入我的SQL数据库。 The data imported does not belong to any predefined class and has to be determined at run time. 导入的数据不属于任何预定义的类,必须在运行时确定。 A lot of forums pointed to using FastMember to doing this but I have been unable to make it work. 许多论坛都指向使用FastMember来执行此操作,但是我一直无法使其工作。

As I do not have a predefined class, I am reading the CSV with the following code: 由于我没有预定义的类,因此我正在使用以下代码读取CSV:

var records = csv.GetRecords<dynamic>().ToList();

I then use fastmember's ObjectReader to structure the data 然后,我使用fastmember的ObjectReader构造数据

using (var reader = ObjectReader.Create(data, copyParameters))
{
     await sbc.WriteToServerAsync(reader);
}

At this stage, I get the error "Specified argument was out of the range of valid values. Parameter name: name" 在此阶段,出现错误“指定的参数超出有效值范围。参数名称:名称”

I'll include the entire code snippet below, but the copyParameters is a string array containing all of the columns exactly as they appear in the CSV. 我将在下面包括整个代码段,但是copyParameters是一个字符串数组,其中包含与CSV中显示的所有列完全相同的所有列。

Small sample CSV used for testing: 用于测试的小样本CSV:

DateTime,Column1,Column2,Column3,Column4 1/8/2014 18:20,1,0,0.3,0 1/8/2014 18:21,1,0,0.3,0 1/8/2014 18:22,1,0,0.2,0 1/8/2014 18:23,1,0,0.2,0 1/8/2014 18:24,1,0,0.2,0 1/8/2014 18:25,1,0,0.2,0 DateTime,Column1,Column2,Column3,Column4 1/8/2014 18:20,1,0,0.3,0 1/8/2014 18:21,1,0,0.3,0 1/8/2014 18:22, 1,0,0.2,0 1/8/2014 18:23,1,0,0.2,0 1/8/2014 18:24,1,0,0.2,0 1/8/2014 18:25,1, 0,0.2,0

这是来自csv阅读器和复制参数的数据

Here is the complete code: 这是完整的代码:

public async Task InsertTimeDataFromImport(IFormFile file, List<ImportCurveGridViewModel> curves, string tableName)
    {
        try
        {
            using (var reader = new StreamReader(file.OpenReadStream()))
            {
                var csv = new CsvReader(reader);
                csv.Configuration.HasHeaderRecord = true;
                csv.Read();

                var records = csv.GetRecords<dynamic>().ToList();

                var copyParameters = curves
                    .Where(c => c.Import)
                    .Select(c => c.CurveName)
                    .ToArray();

                var batchSize = records.Count();

                await _repository.InsertData(tableName, batchSize, records, copyParameters);
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
            throw;
        }

    }

public async Task InsertData(string tableName, int batchSize, IEnumerable<dynamic> data, string[] copyParameters)
    {
        using (SqlBulkCopy sbc = new SqlBulkCopy(_context.Database.GetDbConnection().ConnectionString, SqlBulkCopyOptions.KeepIdentity))
        {
            sbc.DestinationTableName = "[" + tableName + "]";

            sbc.BatchSize = 5000;

            foreach (var param in copyParameters)
            {
                sbc.ColumnMappings.Add(param, param);
            }

            using (var reader = ObjectReader.Create(data, copyParameters))
            {
                await sbc.WriteToServerAsync(reader);
            }
        }
    }

After no progress on this, I decided to bite the bullet and upgrade to .net core 2.1. 在这方面没有任何进展之后,我决定硬着头皮升级到.net core 2.1。 Once I had access to DataTable again, I was able to circumvent this problem quickly. 一旦我再次访问DataTable,便能够快速规避此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的参数超出了有效值的范围。 参数名称:值 - Specified argument was out of the range of valid values. Parameter name: value 指定的参数超出有效值范围。 参数名称:大小 - Specified argument was out of the range of valid values. Parameter name: size 得到“指定的参数超出有效值范围。 参数名称:” - Getting “Specified argument was out of the range of valid values. Parameter name:” 指定的参数超出有效值范围。 参数名称:site - Specified argument was out of the range of valid values. Parameter name: site MEF指定的参数超出有效值范围。 参数名称:site - MEF Specified argument was out of the range of valid values. Parameter name: site 指定的参数超出有效值范围。 参数名称索引。 由ObservableCollection.Add()方法抛出 - Specified argument was out of the range of valid values. parameter name index. thrown by ObservableCollection.Add() method 重现异常:指定的参数超出有效值范围。 参数名称:索引 - Reproduce exception: Specified argument was out of the range of valid values. Parameter name: index 错误指定的参数超出了有效值的范围。 参数名称:DataGridview Row Data Bound 中的索引 - Error Specified argument was out of the range of valid values. Parameter name: index in DataGridview Row Data Bound System.ArgumentOutOfRangeException: &#39;指定的参数超出了有效值的范围。 参数名称:asp.net中的index&#39; - System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net 指定的参数超出了有效值的范围。参数名称:site - 由于创建者更新而导致的错误 - Specified argument was out of the range of valid values. Parameter name: site - Error Due To Creators Update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM