1

I am trying to do a bulk insert with C#. I saw some simple code for the case when number of columns in source csv is same as destination table. I have a csv file which I want to insert into specific columns of a destination table. In my case, the number of columns in destination is greater than the those in csv file. I want to be able to map csv columns to destination columns. Is that possible with SqlBulkCopy? If not, any other options ?

I am using .NET 3.5 and Visual Studio 2008

Source http://www.codeproject.com/Articles/439843/Handling-BULK-Data-insert-from-CSV-to-SQL-Server

StreamReader file = new StreamReader(bulk_data_filename);
CsvReader csv = new CsvReader(file, true,',');
SqlBulkCopy copy = new SqlBulkCopy(conn);
copy.DestinationTableName = tablename;
copy.WriteToServer(csv);
4

1 Answer 1

1

Is that what you're looking for? SqlBulkCopyColumnMapping MSDN Reference

// Set up the column mappings by name.
SqlBulkCopyColumnMapping mapID = new SqlBulkCopyColumnMapping("ProductID", "ProdID");
bulkCopy.ColumnMappings.Add(mapID);
Sign up to request clarification or add additional context in comments.

6 Comments

Will this work for moving data from csv file to table ? right now, my csv file has no column names. but it can be added easily.
This class can map to column indexes (instead names) too. Might be worth a try.
Can you show me what the code will be like if you only want to load a 1 column csv into a 2 column table ? Thanks. I was hoping that SqlBulkCopy is as simple as - set csv source, define mapping somewhere and let SqlBulkBulkCopy do the rest for you. Is it really like that ?
I'd try something like new SqlBulkCopyColumnMapping(0, "MyColumn"). I think the missing column(s) should be a least optional (nullable or with default value). But I have to admit that I used Bulk Copy with tables of the same structure until now (and not with CSV). Just give it a shot, the class will tell you at runtime if it's happy or not.
I was thinking of modifying and using the code here - codeproject.com/Articles/16922/SQL-Bulk-Copy-with-C-Net . this link uses SQL result set instead of csv file as data source. also, it does not do the mapping. Can you show me how to do these two things in the given code ? Thanks.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.