Solved

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

Posted on 2016-10-18
3
100 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 49

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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