Sql 使用C自动导入大型csv文件(~3gb)#
我对此有点陌生,但我的目标是将数据从csv文件导入到sql表中,并为每一行包含额外的值,即文件名和日期。我可以使用实体框架和迭代文件的每一行来完成这项工作,但由于文件的大小,实际完成这项工作需要的时间太长 我正在寻找一种方法来更快地完成此导入。我正在研究可能使用csvhelper和sqlbulkcopy来实现这一点,但不确定是否有方法传递每行所需的附加值Sql 使用C自动导入大型csv文件(~3gb)#,sql,csv,sqlbulkcopy,csvhelper,Sql,Csv,Sqlbulkcopy,Csvhelper,我对此有点陌生,但我的目标是将数据从csv文件导入到sql表中,并为每一行包含额外的值,即文件名和日期。我可以使用实体框架和迭代文件的每一行来完成这项工作,但由于文件的大小,实际完成这项工作需要的时间太长 我正在寻找一种方法来更快地完成此导入。我正在研究可能使用csvhelper和sqlbulkcopy来实现这一点,但不确定是否有方法传递每行所需的附加值 public void Process(string filePath) { InputFilePath = file
public void Process(string filePath)
{
InputFilePath = filePath;
DateTime fileDate = DateTime.Today;
string[] fPath = Directory.GetFiles(InputFilePath);
foreach (var file in fPath)
{
string fileName = Path.GetFileName(file);
char[] delimiter = new char[] { '\t' };
try
{
using (var db = new DatabaseName())
{
using (var reader = new StreamReader(file))
{
string line;
int count = 0;
int sCount = 0;
reader.ReadLine();
reader.ReadLine();
while ((line = reader.ReadLine()) != null)
{
count++;
string[] row = line.Split(delimiter);
var rowload = new ImportDestinationTable()
{
ImportCol0 = row[0],
ImportCol1 = row[1],
ImportCol2 = TryParseNullable(row[2]),
ImportCol3 = row[3],
ImportCol4 = row[4],
ImportCol5 = row[5],
IMPORT_FILE_NM = fileName,
IMPORT_DT = fileDate
};
db.ImportDestinationTable.Add(rowload);
if (count > 100)
{
db.SaveChanges();
count = 0;
}
}
db.SaveChanges();
//ReadLine();
}
}
}
static int? TryParseNullable(string val)
{
int outValue;
return int.TryParse(val, out outValue) ? (int?)outValue : null;
}
}
在数据库外部执行文件处理,并生成一个准备加载的顺序文件。使用数据库实用程序将此文件导入数据库而不进行编码。如果将
count>100
修改为count>1000
或count>10000
,性能会发生什么变化?您是否已分析以确定您在哪里花费时间?CsvHelper
解析文件的速度不会快于reader.ReadLine()
。它将要做的是正确处理转义。按原样line.Split(分隔符)如果文本字段包含嵌入的分隔符,则代码>将无法正常工作CsvHelper
(或任何其他合适的CSV解析器)将自动为您处理该问题。