When we need to insert a bulk copy of data in to SQL.we have
to follow the SqlBulkCopy class to make this process
to be simple.
Let us illustrate with an example. Suppose we have excel
file or csv file now we need to insert all the data to SQL.
In such cases we must follow the below method.
Initially we need to get all the data from the excel file or
CSV file. In a previous article I have written about to import the data from
excel file and CSV file.
Once you imported all the data into DATATABLE.
Now you have all the data in databale, instead of using loop method you have to
use SqlBulkCopy class.
using (SqlBulkCopy
copy = new SqlBulkCopy(cn))
{
copy.ColumnMappings.Add(0, 0);
copy.ColumnMappings.Add(1, 1);
copy.ColumnMappings.Add(2, 2);
copy.ColumnMappings.Add(3, 3);
copy.ColumnMappings.Add(4, 4);
copy.DestinationTableName
= "product";
copy.WriteToServer(dt);
}
|
That’s it …….
Post your comments here……
0 comments:
Post a Comment