Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am trying to insert thousands of records from csv file to sql table.My csv file having more than hundred columns. I found so many articles about sql bulk copy. but in the given examples there are 3 or 4 columns max. so mapping csv columns with database table columns is a big headache in my case.Plz anyone suggest me how to perform bulk copy when csv having multiple columns.

articles i found
SqlBulkCopy- Bulk Insert records and Update existing rows if record exists using C# and VB.Net[^]

What I have tried:

I am trying to insert thousands of records from csv file to sql table.My csv file having more than hundred columns. I found so many articles about sql bulk copy. but in the given examples there are 3 or 4 columns max. so mapping csv columns with database table columns is a big headache in my case.Plz anyone suggest me how to perform bulk copy when csv having multiple columns.
Posted
Updated 26-Apr-17 9:09am
v2
Comments
CHill60 26-Apr-17 9:33am    
The "What I have tried:" section is for the code you are using.
Mapping the columns to the CSV may be a headache but at least you only have to do it once.
It is possible to transpose the headings from the CSV using Excel and to generate code in that excel spreadsheet. I have successfully written thousands of lines of mapping code using that technique in a matter of moments.
PIEBALDconsult 26-Apr-17 9:54am    
Look into SSIS or other ETL tools.
Or use ADO.net -- the System.Data.OleDb classes -- with the ACE engine to read CSV like a database.

I have used the BCP utility, SqlBulkCopy, and ADO.net to do ETL work in the past, but I've spent the last five years doing ETL work with SSIS -- with more and more of it being from CSV files.

One main lesson I've learned in the last twenty years (and I recall other members of this site saying the same thing):
Don't try to convert data from text to other dataypes during the initial ETL process. Get the data into the database first (as text) and then do further processing on it.

A further benefit of doing that is that you can then write a utility that reads a CSV and produces a mapping file that BCP can use to load the data.
 
Share this answer
 
The way you use BulkCopy on few columns doesn't differ to BulkCopy on multiple columns, but...

You have to use ADO.NET[^] - OleDb[^].

How?

  1. Create schema.ini file in which you have to define the number of columns
    See:
    Schema.ini File (Text File Driver) | Microsoft Docs[^]
    How to: Add a Schema Definition to a Text File Data Source[^]
    Much ADO About Text Files[^]
    My past answer with sample code: Read Text File Specific Columns[^]
  2. Create OleDbConnection[^] and OleDbCommand[^]
  3. Use OleDbReader[^] to Load[^] data into DataTable
  4. Now, connect your programme to Sql server database and copy the data using SqlBulkCopy class[^]


That's all!

For further details, please see:
ADO.NET Code Examples[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900