【问题标题】:Move CSV file to database将 CSV 文件移动到数据库
【发布时间】:2021-07-06 16:25:36
【问题描述】:

我在第一行有 CSV 文件

offerId;monthlyFee;newContractsForMonth;sameContractsForMonth;CancelledContractsForMonth

从第二行到最后我有两列 - 例如:

Second row first column 1;38 Second row second column 66;98;68;28
Third row first column 2;10 third row second column 11;99;180;15

等等

我写了这段代码

using (var streamReader = new StreamReader(@path))
{
    using (var csvReader = new CsvReader(streamReader, config))
    {
        var records = csvReader.GetRecords<OfferWithoutInteger>().ToList();

        foreach (var record in records)
        {
            record.offerId = Regex.Replace(record.offerId, ",", "");
            record.monthlyFee = Regex.Replace(record.monthlyFee, ",", "");
            record.newContractsForMonth = Regex.Replace(record.newContractsForMonth, ",", "");
            record.sameContractsForMonth = Regex.Replace(record.sameContractsForMonth, ",", "");
            record.CancelledContractsForMonth = Regex.Replace(record.CancelledContractsForMonth, ",", "");
        }

        await this._context.AddRangeAsync(records);
        await this._context.SaveChangesAsync();
    }
}

并设法将我的数据以字符串格式放入数据库。我的问题是如何合并两列以及如何将数据从字符串转换为整数。

谢谢

【问题讨论】:

  • 如果你用整数而不是字符串创建一个类会发生什么? (Offer_with_Integers)?
  • 你能展示一个实际文件的样本吗?
  • 是的,这是我第一次尝试,但为了转换数据类型,我应该合并两列,因为当我将分隔符设置为“;”时当两列连接时,它们会给我 45,15 这样的数据,但它必须是 4515
  • ibb.co/Jm7wk2D 是的,这是示例

标签: c# asp.net database csv csv-import


【解决方案1】:

找到了解决方案。我用过

Map(row => row.CustomClassField).Name("CSVColumnName").Convert(row => int.Parse(row.Row.GetField("CSVColumnName").Replace(",","")));

下面是代码。

public async Task<IActionResult> Index()
        {
            var @path = @"pathToFile";
            var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ";", Encoding = Encoding.UTF8 };

            var offersWithIntegerValues = new OfferWithIntegers();
            using (var streamReader = new StreamReader(@path))
            {
                using (var csvReader = new CsvReader(streamReader, config))
                {
                    csvReader.Context.RegisterClassMap<OffersWithIntegersMap>();
                    var records = csvReader.GetRecords<OfferWithIntegers>().ToList();                        
                }
            }

            return View();
        }

        public class OffersWithIntegersMap : ClassMap<OfferWithIntegers>
        {
            public OffersWithIntegersMap()
            {
                Map(row => row.offerId).Name("offerId").Convert(row => int.Parse(row.Row.GetField("offerId").Replace(",","")));
                Map(row => row.monthlyFee).Name("monthlyFee").Convert(row => int.Parse(row.Row.GetField("monthlyFee").Replace(",","")));
                Map(row => row.newContractsForMonth).Name("newContractsForMonth").Convert(row => int.Parse(row.Row.GetField("newContractsForMonth").Replace(",","")));
                Map(row => row.sameContractsForMonth).Name("sameContractsForMonth").Convert(row => int.Parse(row.Row.GetField("sameContractsForMonth").Replace(",","")));
                Map(row => row.CancelledContractsForMonth).Name("CancelledContractsForMonth").Convert(row => int.Parse(row.Row.GetField("CancelledContractsForMonth").Replace(",","")));
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    相关资源
    最近更新 更多