Solved

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

Posted on 2016-10-18
3
53 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks For ur suggestions Experts
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now