本文介绍了我需要带有 SQLBulkcopy 的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要进度条.我正在通过文本文件将数据上传到 SQL Server 中,但它需要很多时间,而且我使用了后台工作人员,但无法正常工作,所以我只需要知道有什么方法可以将进度条与 SQL 批量复制一起使用它告诉我插入了 2000 条记录?这是我的代码:

I need progress bar for my application. I am uploading data through text file into SQL Server but it takes a lot of time and also i used background worker for the same but that's not working properly so i just need to know is there any way i can use progress bar with SQL Bulk Copy and it tells me that's 2000 records inserted?Here is my code:

 public void bulkinsert(string tablename, DataTable dt)
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlBulkCopy blkcopy = new SqlBulkCopy(con);
        blkcopy.DestinationTableName = tablename;
        blkcopy.BatchSize = dt.Rows.Count;
        blkcopy.BulkCopyTimeout = 1500;
        blkcopy.WriteToServer(dt);
        blkcopy.Close();

    }

非常感谢您的回复.

推荐答案

您需要处理 SqlBulkCopy.SqlRowsCopied 事件其中

You need to handle the SqlBulkCopy.SqlRowsCopied event which

每次达到 NotifyAfter 指定的行数时发生财产已被处理.

我也会使用 Async Await 并使用 Progress 而不是后台工作者

I would also use Async Await and use a Progress<T> instead of a background worker

这篇关于我需要带有 SQLBulkcopy 的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 05:53
查看更多