Solved

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

Posted on 2016-10-18
3
112 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
[X]
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
3 Comments
 
LVL 50

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 27

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

687 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