SQL Bulk Insert With Table Column Mapping In C#

Bulk insert into SQL Server after mapping datatable columns to SQL Server table columns.

We can use SqlBulkCopy class to bulk copy data. The ColumnMapping property returns a collection of SqlBulkCopyColumnMapping items. If the data source and the destination table have the same number of columns, and the ordinal position of each source column within the data source matches the ordinal position of the corresponding destination column, the ColumnMappings collection is unnecessary. However, if the column counts differ, or the ordinal positions are not consistent, you must use ColumnMappings to make sure that data is copied into the correct columns.
Here is the code sample: 
  1. public void getdata_mstoms(string[,] columlist,string D_Conn, string D_Table,DataTable dt)  
  2. {  
  3. using (SqlConnection cn = new SqlConnection(D_Conn))  
  4. {  
  5. cn.Open();  
  6. using (SqlBulkCopy copy = new SqlBulkCopy(cn))  
  7. {  
  8. for (int i = 0; i < columlist.Length / 2; i++)  
  9. {  
  10. if (columlist[i, 1] != null)  
  11. {  
  12. copy.ColumnMappings.Add(columlist[i, 0].ToString(), columlist[i, 1].ToString());  
  13. }  
  14. }  
  15. copy.DestinationTableName = D_Table;  
  16. copy.WriteToServer(dt);  
  17. }  
  18. }  
  19. }  
Learn more details here: Bulk Copy In SQL Server Using C#