【发布时间】:2020-01-31 16:27:08
【问题描述】:
我正在使用 C# asp.net core、linQ 和 T-SQL 中的托管服务。
我需要在我的数据库中插入一条一条记录。 当然这不是一个快速的操作,但我在这个领域没有那么有经验,所以也许我做错了。 这是我在经理中的代码:
public void StrategyMassive(string foldpathsave)
{
using (IServiceScope scope = _services.CreateScope())
{
List<string> filesreading = new List<string>();
VUContext _context = scope.ServiceProvider.GetRequiredService<VUContext>();
List<string> filesnumber = File.ReadAllLines(foldpathsave).ToList();
filesreading = filesnumber.ToList();
filesreading.RemoveRange(0, 2);
foreach (string singlefile in filesreading)
{
//INTERNAL DATA NORMALIZATION
_repository.ImportAdd(_context, newVUL, newC2, newC3, newDATE);
_repository.Save(_context);
}
}
}
这是我的存储库界面:
public void ImportAdd(VUContext _context, AVuTable newVUL, ACs2Table newC2, ACs3Table newC3, ADateTable newDATe)
{
_context.AVuTable.Add(newVU);
_context.ADateTable.Add(newDATE);
if (newC2 != null)
{
_context.ACs2Table.Add(newC2);
}
if (newC3 != null)
{
_context.ACs3Table.Add(newC3);
}
public void Save(VUContext _context)
{
_context.SaveChanges();
}
}
我知道这一切都很简单,那么我怎样才能加快这个插入的速度呢?
【问题讨论】:
-
BULK操作在这里会快得多,而不是让 C# 一次读取每一行,然后一次插入一行。 -
将插入内容分成更小的批次。 -- mssqltips.com/sqlservertip/5636/…
-
不要这样做,根据你的数据库进行批量插入。
标签: c# sql-server linq tsql asp.net-core