试图找到从.csv到两个表

时间:2015-07-29 13:22:43

标签: c# mysql sql asp.net-mvc csv

我正在构建的应用程序允许用户上传.csv文件,该文件最终将填充Ids匹配的现有SQL表的字段。首先,我使用LinqToCsv和foreach循环将.csv导入临时表。然后我有另一个foreach循环,它将临时表中的字段循环到Ids匹配的现有表中。我能够始终如一地成功地工作的唯一方法是将第二个foreach循环嵌套在第一个:

        [HttpPost]
    public ActionResult UploadValidationTable(HttpPostedFileBase csvFile)
    {
        var inputFileDescription = new CsvFileDescription
        {
            SeparatorChar = ',',
            FirstLineHasColumnNames = true
        };
        var cc = new CsvContext();
        var filePath = uploadFile(csvFile.InputStream);
        var model = cc.Read<Credit>(filePath, inputFileDescription);

        try
        {
            var entity = new TestEntities();
            foreach (var item in model)
            {
                var tc = new TemporaryCsvUpload
                {
                    Id = item.Id,
                    CreditInvoiceAmount = item.CreditInvoiceAmount,
                    CreditInvoiceDate = item.CreditInvoiceDate,
                    CreditInvoiceNumber = item.CreditInvoiceNumber,
                    CreditDeniedDate = item.CreditDeniedDate,
                    CreditDeniedReasonId = item.CreditDeniedReasonId,
                    CreditDeniedNotes = item.CreditDeniedNotes
                };
                entity.TemporaryCsvUploads.Add(tc);

                var idMatches = entity.Authorizations.ToList().Where(x => x.Id == tc.Id);

                foreach (var number in idMatches)
                {
                    number.CreditInvoiceDate = tc.CreditInvoiceDate;
                    number.CreditInvoiceNumber = tc.CreditInvoiceNumber;
                    number.CreditInvoiceAmount = tc.CreditInvoiceAmount;
                    number.CreditDeniedDate = tc.CreditDeniedDate;
                    number.CreditDeniedReasonId = tc.CreditDeniedReasonId;
                    number.CreditDeniedNotes = tc.CreditDeniedNotes;
                }
            }
            entity.SaveChanges();
            entity.Database.ExecuteSqlCommand("TRUNCATE TABLE TemporaryCsvUpload");

            TempData["Success"] = "Updated Successfully";

        }
        catch (LINQtoCSVException)
        {
            TempData["Error"] = "Upload Error: Ensure you have the correct header fields and that the file is of .csv format.";
        }

        return View("Upload");
    }

问题在于速度。搜索7000个条目的SQL表,匹配ID并填写字段大约需要1分49秒。

所以,我看了这个,并认为第二个循环真的不需要嵌套。我把代码改为:

        [HttpPost]
    public ActionResult UploadValidationTable(HttpPostedFileBase csvFile)
    {
        var inputFileDescription = new CsvFileDescription
        {
            SeparatorChar = ',',
            FirstLineHasColumnNames = true
        };
        var cc = new CsvContext();
        var filePath = uploadFile(csvFile.InputStream);
        var model = cc.Read<Credit>(filePath, inputFileDescription);

        try
        {
            var entity = new TestEntities();
            var tc = new TemporaryCsvUpload();
            foreach (var item in model)
            {

                tc.Id = item.Id;
                tc.CreditInvoiceAmount = item.CreditInvoiceAmount;
                tc.CreditInvoiceDate = item.CreditInvoiceDate;
                tc.CreditInvoiceNumber = item.CreditInvoiceNumber;
                tc.CreditDeniedDate = item.CreditDeniedDate;
                tc.CreditDeniedReasonId = item.CreditDeniedReasonId;
                tc.CreditDeniedNotes = item.CreditDeniedNotes;
                entity.TemporaryCsvUploads.Add(tc);
            }

            var idMatches = entity.Authorizations.ToList().Where(x => x.Id == tc.Id);

            foreach (var number in idMatches)
            {
                number.CreditInvoiceDate = tc.CreditInvoiceDate;
                number.CreditInvoiceNumber = tc.CreditInvoiceNumber;
                number.CreditInvoiceAmount = tc.CreditInvoiceAmount;
                number.CreditDeniedDate = tc.CreditDeniedDate;
                number.CreditDeniedReasonId = tc.CreditDeniedReasonId;
                number.CreditDeniedNotes = tc.CreditDeniedNotes;
            }
            entity.SaveChanges();
            entity.Database.ExecuteSqlCommand("TRUNCATE TABLE TemporaryCsvUpload");

            TempData["Success"] = "Updated Successfully";

        }
        catch (LINQtoCSVException)
        {
            TempData["Error"] = "Upload Error: Ensure you have the correct header fields and that the file is of .csv format.";
        }

        return View("Upload");
    }

这一次,它只用了19秒就完成了。第一个是巨大的改进。但是当我检查数据库时,只填充了应匹配的7行中的一行。任何人都可以找到第二个代码块不会填充所有行的原因吗?还是优化第一块的更好方法?谢谢!

0 个答案:

没有答案