.NET c#如何使用linq2db进行大量插入?

时间:2015-12-25 00:53:11

标签: c# linq sqlite linq2db

目前我正在使用Linq2db来管理我的C#应用​​程序的sqlite数据库。现在我正在读取一个24k +行的excel文件,我想知道如何加快我的ETL过程?

for (int row = start.Row; row <= end.Row; row++)
        {
            if (row == 1) // Title row
                continue;

            Stock stock = new Stock(Processor.GetStore(workSheet.Cells[row, 1].Text),
                            Processor.GetProduct(workSheet.Cells[row, 2].Text),
                            int.Parse(workSheet.Cells[row, 6].Text),
                            int.Parse(workSheet.Cells[row, 7].Text),
                            int.Parse(workSheet.Cells[row, 8].Text), 0, true);

            Processor.AddStock(stock, false);

        }

尝试使用linq采用不同的方法,但我的计时结果更差......

            var stocks = (from cell in workSheet.Cells["a:h"]
                      select new Stock(Processor.GetStore(workSheet.Cells[cell.Start.Row, 1].Text),
                            Processor.GetProduct(workSheet.Cells[cell.Start.Row, 2].Text),
                            int.Parse(workSheet.Cells[cell.Start.Row, 6].Text),
                            int.Parse(workSheet.Cells[cell.Start.Row, 7].Text),
                            int.Parse(workSheet.Cells[cell.Start.Row, 8].Text), 0, true)).ToList();

我正在寻找的是这样的:

public static void MassStockInsert(List<Stock> stocks)
    {
        using (var db = new Processor())
        {
            db.Stock
                .insert(stocks);
        }
        Processor.Stocks = ReloadStocks();
    }

1 个答案:

答案 0 :(得分:7)

使用BulkCopy方法。

public static void MassStockInsert(List<Stock> stocks)
    {
        using (var db = new Processor())
        {
            db.BulkCopy(stocks);
        }
        Processor.Stocks = ReloadStocks();
    }