Making BulkCopy Use Column Names Instead of Position

  • Hi Folks,

    I'm writing an application in C# to transfer Excel spreadsheets to SQL tables. I use BulkCopy and it works very well except for one issue:

    It copies data according to column position instead of column name. So if in my spreadsheet I switch the position of two columns, the data will not transfer properly. However, if I explicitly use ColumnMappings.Add("orderId", "orderId"); ColumnMappings.Add("item", "item"); etc... It works just fine! It seems like there should be a way to tell BulkCopy to match the source/destination column names automatically. In fact, I thought this was supposed to be the default behavior.

    Any help would be GREATLY appreciated!

    Thanks,

    Scott

  • There is a way to do it automatically - just enumerate through the names of the columns in your source, adding each one as an explicit bulk copy mapping... I know it's not as automatic as you're looking for, but it is like 3 lines of code, tops.

    Atlantis Interactive - SQL Server Tools
    My blog[/url]
    Why I wrote a sql query analyzer clone

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply