1

实际上,我正在导入Excel文件并将记录保存到DB通过SqlBulkCopy ,一切正常。现在,我需要ProgressBar通过SqlBulkCopy.

我的代码是:

SBC.WriteToServer(dsExcel);
SBC.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
SBC.NotifyAfter = 1;

SqlRowsCopied事件中:

private static void OnSqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
//here i tried calling my progressbar. But I'm unable to call it.                   
}  
4

1 回答 1

1

I think you need to set that value right after you instantiate the object and before you call the WriteToServer Method:

using (SqlConnection conn = new SqlConnection(ConnectionString))
{
    conn.Open();
    using (SqlBulkCopy sqlcpy = new SqlBulkCopy(conn))
    {
        sqlcpy.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied); //<<---- You need to add this here
        sqlcpy.NotifyAfter = 1;
        sqlcpy.BatchSize = batchSize;
        sqlcpy.BulkCopyTimeout = 60;

        using (SqlCommand cmd = new SqlCommand(Sql, conn))
        {
            cmd.ExecuteNonQuery();
            sqlcpy.DestinationTableName = TableName;  //copy the datatable to the sql table
            sqlcpy.WriteToServer(dt);
            return sqlcpy.GetRowsCopied();
        }
    }
}

and it goes without saying but you also need a OnSqlRowsCopied event handler:

private static void OnSqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
   Console.WriteLine("Copied {0} so far...", e.RowsCopied);
}
于 2013-09-23T02:47:02.430 回答