Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

In this tutorial i am going to explain about how to Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC. In my previous tutorial i have explained about

Implementation :

Now we will see the implementation of how to Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC.

For explaining purpose i have created a new asp.net mvc project. After that right click on project and select Manage Nuget Packages and search for Entity Framework

Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

Select Entity Framework latest version and click install.

step 2 of Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

Once entity framework is installed, click on models folder and add a class called Employee.cs

Step 3 of Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

Employee.cs will contain below propertis.

And now again right click on models folder and add a new class called DemoContext.cs. This will inherit from DBContext class. Below is my DemoContext.cs code

And i have added the conection string in web.config file.

Add Controller:

Once context class is created it is time to add controller. Now right click on the controllers folder and choose Add=> Controller option. Name the controller as ImportController.cs

Step 5 of step 2 of Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

Add View:

The controller has a get method with action named Index and post method with action named as Index. Now right click on the action name and add view.

Step 6 of Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

The view contains below information

  1. File Upload control to choose and upload files
  2. Submit button to post the selected file for processing
  3. Viewbag.Message to show any success,error or warning messages

Below is the complete view code.

Below is my complete controller code.

Output:

Now if you run the program you can see the output of how to Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC.

Output of Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

Source Code:

Source code of Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC

Conclusion:

Hope you learned Import Data From Excel To Sql Server Using Entity Framework & ASP.Net MVC. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

Your email address will not be published. Required fields are marked *