• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

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

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
karthik80c
Asked:
karthik80c
1 Solution
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
ZberteocCommented:
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
 
karthik80cAuthor Commented:
Thanks For ur suggestions Experts
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now