3

Is it possible to save a csv file to a SQL Server Database table using C#?

Do need to convert the csv file to Binary format in order to do this?

Is there any other way, like saving it in text format(csv is a basically a txt file)?

I'd appreciate it if you could provide some example code.

5
  • CSV file can be directly imported on SQL Server without using C#. See This Link Commented Sep 13, 2012 at 1:32
  • If you really want to import using C#, here's a project from CodeProject: C# - (CSV Import Export) Commented Sep 13, 2012 at 1:35
  • Can you please clarify if you explicitly don't want to break the CSV into multiple data fields? Depending on your scenario, it might be better to do this rather than keeping a copy of the entire file in the database. But on the other hand, if you just need a copy of the file, the database might not be the best place for it - rather, perhaps your DB could just contain a file-path to the location of the file stored on a filesystem somewhere. Commented Sep 13, 2012 at 1:35
  • I dont want to break the csv file. And I want to save the entire file in one record because there might be hundreds of files coming from different users. Commented Sep 13, 2012 at 1:37
  • Do you need to be able to query against the values in the CSV, or just extract the CSV "on demand" based on selection of other fields from your table? If so, then you could encode it or zip it or whatever, before importing it. But again, is the DB the best place for the file itself, rather than just a reference to the file, if you're not performing DB-like operations on the file as a whole, or on its contents? Commented Sep 13, 2012 at 14:41

3 Answers 3

2

CSV files are just text. You can create a table that has an nvarchar(max) column and store your CSV files in there.

Sign up to request clarification or add additional context in comments.

6 Comments

But I want to save the whole file in one single record. Can you provide some code.
I don't understand. You can insert your entire CSV file into a single column of type nvarchar(max).
@Henry, perhaps this ADO.NET C# tutorial will help. See "Inserting Data."
@Henry, a sample insert statement: insert into CSV_TABLE (ID, CSV_TEXT) values (12345, "csv,row,one\ncsv,row,two\ncsv,row,three");.
@Henry, a table to match the insert statement: create table CSV_TABLE (ID numeric, CSV_TEXT nvarchar(MAX));.
|
1

You have another helpful C# sample for import and export of CSV file with SQL Server. Below is the link:

http://www.codeproject.com/Articles/30705/C-CSV-Import-Export

Comments

0

You can use the data import wizard I believe and point it at a csv file. This should create a table that matches the structure of your imported csv

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.