简体   繁体   English

C#SqlBulkCopy的快速方法

[英]Fast approach to C# SqlBulkCopy

I'm reading data from huge csv file (5,000,000 rows and hundreds of columns) and using SqlBulkCopy for writing data into sql table. 我正在从巨大的csv文件(5,000,000行和数百列)中读取数据,并使用SqlBulkCopy将数据写入sql表。 I'm trying to speed up performance and found WriteToServerAsync method in SqlBulkCopy class. 我试图提高性能,并在SqlBulkCopy类中找到WriteToServerAsync方法。 It is much faster than WriteToServer method but cannot see any data in sql table. 它比WriteToServer方法快得多,但无法在sql表中看到任何数据。

public static void InsertDataIntoSQLServerUsingSQLBulkCopy_2(DataTable dtable, string sqlTableName, Int32 batch_size, string connString)
{
    try
    {
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connString))
        {
            bulkCopy.DestinationTableName = sqlTableName;

            try
            {
                bulkCopy.BulkCopyTimeout = 0;
                bulkCopy.BatchSize = batch_size;
                bulkCopy.WriteToServerAsync(dtable);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Environment.Exit(0);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
        Environment.Exit(0);
    }
}

int batchsize = 0; 
while (!reader.EndOfStream) 
{ 
    string[] line = reader.ReadLine().Split('\t'); 
    datatable.Rows.Add(line); 
    batchsize += 1; 
    if (batchsize == flushed_batch_size) 
    { 
        InsertDataIntoSQLServerUsingSQLBulkCopy_2(dt, tabName, flushed_batch_size, connString);
        dt.Rows.Clear();
        batchsize = 0; 
    } 
    rows += 1; 
}
InsertDataIntoSQLServerUsingSQLBulkCopy_2(dt, tabName, flushed_batch_size, connString);
dt.Rows.Clear();

Is there anything I'm missing in my InsertDataIntoSQLServerUsingSQLBulkCopy_2 method? 我的InsertDataIntoSQLServerUsingSQLBulkCopy_2方法是否缺少任何内容?

My answer doesn't address your performance problems. 我的答案没有解决您的性能问题。 It's to hopefully help you determine why the data isn't showing up. 希望能帮助您确定为什么不显示数据。

Patrick mentioned the first problem in the comments. 帕特里克在评论中提到了第一个问题。 However, make sure you change the signature to include Task instead of void : 但是,请确保将签名更改为包括Task而不是void

public static async Task InsertDataIntoSQLServerUsingSQLBulkCopy_2(
    DataTable dtable, string sqlTableName, Int32 batch_size, string connString)

Secondly, one potential reason you won't see results - or see inconsistent ones - is because you're using Environment.Exit(0) in your catch blocks. 其次,您看不到结果或看到不一致结果的一个潜在原因是,因为您在catch块中使用了Environment.Exit(0)

Environment.Exit is different to a return statement in that it will terminate an application, and all associated threads, immediately. Environment.Exitreturn语句的不同之处在于,它将立即终止应用程序以及所有关联的线程。 This could be a reason why you're not seeing any data. 这可能是您看不到任何数据的原因。

Lastly, you need to decide how you want to handle the data import. 最后,您需要确定如何处理数据导入。 Are you fine with the import process continuing if the insertion of a row throws an exception? 如果插入行引发异常,继续导入过程是否还可以? The way you've written that nested try..catch gives the impression that you want it to catch an error but continue. 嵌套try..catch编写方式给人的印象是,您希望它捕获错误但继续。 However, the call to Environment.Exit effectively makes that try..catch redundant, so it just becomes code duplication at that point. 但是,对Environment.Exit的调用有效地使try..catch成为多余的,因此在那时它就变成了代码重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM