Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello,
 
I have a website project with C # language. my idea is to use the FileUpload tool of Visual Studio to import files to my application. what i need is to import a CSV file that contains dozens of data, in order to insert these data in my database.
an example is I have a table "Customer" which has the following structure:
id_custumer int IsIdentity,
name varchar (100),
surname varchar (100),
age int,
locality varchar (200)

as I said, I have created a csv file that contains various customer data. I want, through FileUpload, import this file into my application and adding the data to my table "customer".

any idea how to do this?
Posted
Comments
OriginalGriff 20-Aug-13 5:47am    
What have you tried?
Where are you stuck?
fasher_the_one 20-Aug-13 5:52am    
as I'm giving my first steps with the visual studio, I'm still learning a lot. this is my first case where I need this solution. what I mean is that it has not moved forward at all in this problem. what I've got is just a page containing an FileUpload. the rest I still do not know how to solve.
OriginalGriff 20-Aug-13 6:02am    
OK, it's not too complex: just take it in easy stages.
First off, can you get the FileUpload to load a file into your program (Whether you store it as a file or keep it as data in your code)?
fasher_the_one 20-Aug-13 6:12am    
Let me explain by steps.
I intend to import the csv file for my project, but I want to store in the database the data contained in that file and not the file. imagines that the csv file has 50 customers data, then when I import this file into my project, I plan to add these 50 data in the table "customer" of my database
OriginalGriff 20-Aug-13 6:55am    
:laugh:
Yes I kinda figured that...
So if you can upload the file, you need to process the CSV data into fields and rows, then add it to your DB.
Can you process the CSV once it is uploaded yourself?

Reading a CSV file is a very basic concept and there are thousands of links on how to do it if you use google.

Once you have a tool that can read the CSV the next stage is to insert the data in the your database (I am making an assumption that it is an SQL database, but if it is not then it should still be fairly straight forward). You need to google SQL Insert Statement and the SQL Command object (.Net).

Once you have those two working, then all you need is a file upload mechanism in your website that sends the file to the server and triggers the import once the file is transferred. Again this is easy to find via Google, I should know I had to look up the same thing about 6 weeks ago as I completely forgot how to do it.

You are not doing anything hard here, nor anything that has not been done 1000's of times before, so try actually searching for stuff rather than expecting the code to be handed to you.

I suggest you look up CSV helper as a tool to help with reading the CSV file, and make sure you encode some validation into the process incase people pass you incorrectly formatted files.
Also look up SQLParameters to help prevent against SQL Injection attacks via the file upload process.


Once you basic processes are in place, I suggest you consider the limitations, if you are uploading 1000's of records you might want to consider asynchronous processing of the file so you can inform the user of progress, or just have the file submitted and run the import on a background thread and send out an e-mail once it is complete/failed.
 
Share this answer
 
v2
 
Share this answer
 
Please check here for an example
 
Share this answer
 
Comments
fasher_the_one 20-Aug-13 6:14am    
obrigada,

mas eu não pretendo armazenar o ficheiro em si, mas sim os dados contidos nesse ficheiro (csv)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900