How to reducing time for bulk copying data from different data table

Gani_tpt 1,506 Reputation points
2022-01-19T09:28:48.733+00:00

I have the dataset and it contains different table with different table structure.

Every data table will have more than 70 thousand records and more.

i am trying to move all the data table records using bulk copying method.

I feel that, bulk copying method also will be taking more time to move all the records to different table.

for example,

If i want to move all 70 thousands records in different table, nearly taking 5 to 10 mins.

what is the alternative way to move from data table to Sql table in easy process.

below is my complete code and give us the better solution to handle this time taking issue.

SqlBulkCopy SqlBCopy1 = new SqlBulkCopy(con);
SqlBulkCopy SqlBCopy2 = new SqlBulkCopy(con);
SqlBulkCopy SqlBCopy3 = new SqlBulkCopy(con);
for (int i = 1; i <= 70000; i++) 
{
   SqlBCopy1.ColumnMappings.Clear();
   SqlBCopy2.ColumnMappings.Clear();
   SqlBCopy3.ColumnMappings.Clear();

   connection.Open();
      try
         {
           DataSet.ReadXml(dtReader);

                        SqlBCopy1.DestinationTableName = "tblEmpDetails";
                        foreach (DataColumn col1 in DataSet.Tables[1].Columns)
                            SqlBCopy1.ColumnMappings.Add(col1.ColumnName, col1.ColumnName);
                        SqlBCopy1.WriteToServer(DataSet.Tables[1]);

                        SqlBCopy2.DestinationTableName = "tblEmpSalary";
                        foreach (DataColumn col2 in DataSet.Tables[2].Columns)
                            SqlBCopy2.ColumnMappings.Add(col2.ColumnName, col2.ColumnName);
                        SqlBCopy2.WriteToServer(DataSet.Tables[2]);

                        SqlBCopy3.DestinationTableName = "tblEmpProject";
                        foreach (DataColumn col3 in DataSet.Tables[3].Columns)
                            SqlBCopy3.ColumnMappings.Add(col3.ColumnName, col3.ColumnName);
                        SqlBCopy3.WriteToServer(DataSet.Tables[3]);
}
catch (WebException ex)
 {

 }
                finally
                {
                    connection.Close();
                }

}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2022-01-19T15:35:56.2+00:00

    For bulk copy to run at max speed, you need to drop the indexes on the target tables. A common practice, is to have a staging table that you bulk load, then use set transactions to move data from staging to live.


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2022-01-19T14:46:34.997+00:00

    Your design goes through a C# project. SQL server has the capability to import XML. Skip the extra C# overhead and import the data into SQL using native features. Read the docs...

    Examples of bulk import and export of XML documents (SQL Server)

    0 comments No comments