newbs
newbs

Reputation: 19

Upload CSV File then Mapped to Tables in Database

How can we upload a CSV file through web(ASP.NET MVC C#) and mapped the column in CSV table to our tables in database

Example:

CSV File:
Username,Address,Roles,Date

How to add all the value in 'Username' column to User Table, Name Column ?

Value in 'Address' column to AddrDet Table, Address Column?

Value in 'Roles' column to RolesDet Table, Roles Column?

AND choose the CSV column to be added to database? (So not all column in CSV will be taken)

using ASP.NET MVC C#

Because all I know is when the CSV uploaded, it will create DataTable specially for CSV and all the column in CSV will be uploaded

Thank You

I'm using MVC and EF DB FIRST

Upvotes: 0

Views: 4605

Answers (1)

Tatranskymedved
Tatranskymedved

Reputation: 4371

This questions is being marked as duplicate of Upload CSV file to SQL server

I don't feel (& don't think) that the question is related to or has completely same topic, so I'm answering this. I have myself marked question as too broad, as there is too much to explain.

Also I will add some links to the question, however they are not here to fill the answer, only to give OP an idea what question/topics to look for himself.


Short explanation:

Usually when You want to import data (CSV file) into database, You already got structure & schema of data (and Database). There is existing TableA and TableB, where exist some columns inside. If You want to dynamically create new columns/update schema of DB based on CSV file, this is an uneasy work (normally is not happening).

C#/ASP/.NET application is working in a way where You give it an input (from users' clicks, data load, task scheduler passed some time checkpoint) and the APP do the work.

Typical job looks like: "We got data in this format, APP have to convert them to the inner representation (classes) and then insert them to the server". So You have to write an ASP page, where You allow user to paste/load the file. E.g.: File Upload ASP.NET MVC 3.0

Once You have loaded the file, You need to convert the CSV format (format of stored data) into Your internal representation. Which means create Your own class, with some properties and convert (Transform) CSV into the classes. E.g.: Importing CSV data into C# classes

Since You have this data inside classes (objects - instances of classes), You can work with them and carry out some internal work. This time we are looking for CRUD (Create/Read/Update/Delete) operations against SQL database. First You need to connect to SQL server, choose database and then run the queries. E.g.: https://www.codeproject.com/Articles/837599/Using-Csharp-to-connect-to-and-query-from-a-SQL-da

Plenty of developers are too lazy to write the queries themselves and they like more Object-Oriented access to this sort of problem. They are using ORM - Object-relation mapping, which allows users to have same class/object schema in Database and in the Application. One example for all is Entity-Framework (EF). E.g.: http://www.entityframeworktutorial.net/


As You can see this topic is not so easy and requires knowledge in several parts of programming.

Upvotes: 3

Related Questions