Click here to Skip to main content
15,914,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use Bulk Copy to multiple insert a large amount of data from one server to another. The problem is that I don't a design that would require my first server (the one that sends the data) to know the Connection String for the second server. So I have a program that transmits the data from the origin to the destination, and destination server knows the new server's connection string. Currently I'm working with multiple inserts - which is extremely slow. How can I improve my performance according to the specified requirements?
Posted

This article [^]uses bulkcopy.
 
Share this answer
 
C#
public void ParallelLoadTest()
{
   
    List<datatable> tables = GetTestData(); 

   
    Parallel.ForEach(tables, table =>
        {
            BulkLoadData(table);
        }
    );
}

public void BulkLoadData(DataTable dt)
{
     using(SqlConnection conn = new SqlConnection("{removed connectionstring}"))
     using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.TableLock, null))
     {
          bulkCopy.DestinationTableName = "Dataload";
          bulkCopy.BulkCopyTimeout = 60;
          bulkCopy.ColumnMappings.Add("FieldA", "FieldA");
          bulkCopy.ColumnMappings.Add("FieldB", "FieldB");
          conn.Open();
          bulkCopy.WriteToServer(dt);
          bulkCopy.Close();
     }
}</datatable>
 
Share this answer
 
v2
Comments
ShacharK 24-Oct-11 1:47am    
Thanks, I figured out by myself how use the bulk copy interface.

My question is actually, how can I pass the data within the DataTable first to another location within my network that is familiar with the destination's actual connection string, and would be the one that will use the bulkcopy code (using its already active connection to the destination databaes).

I'm afraid that DataTable can not be binary serialized, is there a better way to do it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900