简体   繁体   中英

Doing a bulk insert with C#?

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM