Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Is there any Easy way to copy CSV to SQL Server using C#

Posted on 2016-10-18
3
90 Views
Last Modified: 2016-10-19
Hi Experts ,

Lot of Internet solutions are trying to create a Data table first and map the column SQL bulk instances and try to import the CSV . What if i have 30 to 40 columns . Do i need to all those columns in data table and SQL bulk copy Instances. Is there any other easy way on this
0
Comment
Question by:karthik80c
3 Comments
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 41847841
You don't need to reinvent the wheel. SQL Server has a bulk copy utility called BCP. It's an .exe file so it can be called from a C# program. Check the examples in the link, they are with .csv.
Or you can execute BULK INSERT command from the database. There's also a .csv example from the link.
1
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 41848355
BULK is not meant to be called from C# as it is a command line utility that runs in Windows. If you want to use C# then use C# objects and features to import a CSV, there is nothing wrong with that, but for a task that simple I don't think you need to go that way.

If this is a one time or even once in a while process then the simplest way is to use the SQL Management Studio Import/Export wizard and that will create the table for you based on the structure of the CSV file, providing it has columns header row. If not it will simply create a table with columns having general names like Col01, col02...

BCP utility:
https://www.simple-talk.com/sql/database-administration/working-with-the-bcp-command-line-utility/

Import wizard:
https://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/
1
 

Author Comment

by:karthik80c
ID: 41850477
Thanks For ur suggestions Experts
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question