Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Using SQL bulk copy with your LINQ-to-SQL datacontext

, 23 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
If the order of your columns is different in the LINQ to SQL class from that in the database, then you also need to add a column mapping to the bulk copier. Otherwise you get mysterious errors in some of the loaded columns!In my simple fix, I assume that the names match even if the order may...

If the order of your columns is different in the LINQ to SQL class from that in the database, then you also need to add a column mapping to the bulk copier. Otherwise you get mysterious errors in some of the loaded columns!

In my simple fix, I assume that the names match even if the order may have been messed about with. In this case you can add the following:

// Assume that the property name matches that in the database.
...
 
table.Columns.Add(new DataColumn(property.Name, propertyType));
 
bulkCopy.ColumnMappings.Add(
  new SqlBulkCopyColumnMapping(property.Name, property.Name));
...

For extra credit, the mapping could be pulled out of the LINQ attributes.

License

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

Share

About the Author

RupertC

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411028.1 | Last Updated 23 Feb 2012
Article Copyright 2012 by RupertC
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid