Introduction
Inserting multiple records in a database is the most common and important task in almost all application. There are inbuilt classes in .NET which support bulk insert which helps to insert multiple records in Database. Recently, I worked on the Bulk insert task and below are the steps and code which help you to achieve insertion of multiple records in Database.
First of all, we need to create DataTable which has identical column names and in the same sequence.
Note that we need the same Database Table and in the same sequence which is in datatable.
After that, fill the datatable with data. Here, I am inserting dummy data of 10000 records.
Now, you are ready with the data of 10000 records which we need to insert. For that, follow the below steps.
Now, the most important task is of MAPPING the columns of Datatable to Database Table. We need to MAP each column as below,
Common Errors
- "The given ColumnMapping does not match up with any column in the source or destination. "
Solution
Check the database table names case sensitive. Also, check the sequence of the column.
- There is another common and confusing error "Error-the given value of type String from the data source cannot be converted to type nvarchar of the specified target column."
Solution
Don't worry, please check the Length of the Column, if the length of data is bigger than column length, it will give this conversion error. increase the length of the column and you will be good.
Happy Coding !!!