将CSV导入SQL-Server-性能我创建了一个小项目,允许用户通过Entity-Framework将CSV文件导入SQL。主要程序如下:using(TextFieldParsertx=newTextFieldParser(file,Encoding.UTF8)){tx.TextFieldType=FieldType.Delimited;tx.SetDelimiters(";");tx.ReadLine();//第一行是标题,不需要while(!tx.EndOfData){decimaldecTmp;intintTmp;字符串[]字段=tx.ReadFields();//将每个字段转换为适当的类型,如果可为空ReportInfo("VerarbeiteNummer:"+fields[(int)ConnectionEvaluationFileField.PhoneNumber].Trim());Verbindunganalysecon=newVerbindunganalyse();con.Auswertungszeitraum=fields[(int)ConnectionEvaluationFileField.EvaluationTimeSpan].TrimStart('0');con.Betrag_inkl_MWST_CHF=Decimal.Parse(字段[(int)ConnectionEvaluationFileField.Cost]);如果(decimal.TryParse(fields[(int)ConnectionEvaluationFileField.DataInMb],outdecTmp))con.Daten_MB=decTmp;con.Durchwahlnummer=Int64.Parse(字段[(int)ConnectionEvaluationFileField.PhoneNumber]);con.Produkteigenschaft=fields[(int)ConnectionEvaluationFileField.ProductProperty];如果(Int32.TryParse(fields[(int)ConnectionEvaluationFileField.Messages],outintTmp))con.SMS_MMS_Anzahl=intTmp;con.TelefonieDauer=TimeSpan.Parse(字段[(int)ConnectionEvaluationFileField.CallLength]);con.Untergruppe=fields[(int)ConnectionEvaluationFileField.SubGroup];如果(Int32.TryParse(fields[(int)ConnectionEvaluationFileField.Connections],outintTmp))con.Verbindungen_Anzahl=intTmp;container.Verbindunganalyse.Add(con);它是德语的,但我认为代码非常清楚:我使用TextFieldParser读取数据,创建一个新的Entitiy并解析/检查属性中的字段问题:客户曾经有一个Access-Solution花费了20-30秒导入数据,我的解决方案,我需要2-3分钟来导入2个文件和5k数据集。我发现了一些像https://efbulkinsert.codeplex.com/这样的BulkInsert,但我们在这里讨论的是两个文件中的10k数据集。我删除了ReportInfo等,但我看不到使这项工作更快的方法。你认为用EF制作这样的东西会更快吗?或者TextFieldParser非常慢,我需要在那里检查?我知道这是一个旧帖子,但我一直在试验这个。我使用以下代码尝试了EntityFrameworkSeederNuGet包和裸机SQL批量插入;CREATETABLE[dbo].[TempTable]([Field]typeconstraints,etc...)GOBULKINSERTTempTableFROM'filePath'WITH(FIRSTROW=2,FIELDTERMINATOR=',',ROWTERMINATOR='rn')GOINSERTINTO复制代码[FinalTable]SELECT[Fields]FROM[TempTable]GODROPTABLE[TempTable]Seeder方法在67秒内插入10000条记录,batchinsert方法耗时223毫秒。这真的很简单。以上就是C#学习教程:将CSV导入SQL-Server——性能分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
