Retrieve Data From Database in ASP.Net Web API

Introduction

This article explains how to retrieve data from the database in the ASP. NET Web API. For retrieving the data we use the ADO.NET Entity Data Model. The Entity Data Model is an entity relationship data model. The Entity Data Model works with the SQL and other query Languages designed for invoking the declarative queries and updates in terms of entities and relationships.

The Entity Data Model gives links and information for working with the data used by the applications built on the Entity Framework.

The following is the procedure for retrieving the data from the database.

Step 1

First we create a table and insert some data into this table.

  1. Start SQL Server 2012.
  2. Select "New Query".
  3. Create a database and a table.

Use the following queries:

  1. create database Demo  
  2. use Demo  
  3. create table info(ID int IDENTITY PRIMARY KEY,Name varchar(50),Appointment varchar(50),Technology varchar(50), Task varchar(MAX) )  
  4. Insert into info values ('A.P godse','Software Develpoer','.NET, Database''Computer Graphics')  
  5. Insert into info values ('yashwant Kanitker','Programmer','.NET, PHP, JSON','Let us C')  
  6. Insert into info values ('E balaguruswami','Programmer''.NET, Javascript','Object Oriented System')  
  7. Insert into info values ('R.S, Aggrawal','Software Developer',' Web API, MVC4','Design Analysis and Algorithm') 

 

Step 2

  • Create ASP.NET Web API application.

  • Start Visual Studio 2012.

  • From the start window select "New Project".

  • In the Template Window select "Installed" -> "Visual C#" -> "Web".

  • Select "ASP.NET MVC 4 Web Application" and click on "OK".

    re3.jpg

  • From the "MVC4 Project" window select "Web API".

    re4.jpg

Step 3

Now add the Entity Framework to our application.

  • In the Solution Explorer.

  • Right-click on the Models folder then select "Add" -> "New Item".

  • From the window select "Installed" -> "Visual C#" -> "Data".

  • And then select "ADO.NET Entity Data Model".

    re5.jpg

  • Click on the "Ok" button.

Step 4

Now open the Entity Data Model Wizard.

  • Click on "Generate from Database".

    re6.jpg

  • Click on "New Connection".

  • Enter your server name.

  • Choose your authentication, here we use the SQL Server Authentication, then we enter the user name and password.

  • Select your database.

    re7.jpg

  • Click on the "Ok" button.

Step 5

In this step:

  • We select the "Yes, include the sensitive data in the connection string."

  • Set the name of entity connection string in the Web.config file.

    re8.jpg

  • Click on the "Next" button.

Step 6

In this step:

  • Choose your table.

  • Write the name of the Model Namespace

    re9.jpg

  • Click on the "Finish" Button.

After adding the model, the Solution Explorer looks like this:

re2.jpg

The table diagram looks as in the following:

re.jpg


Step 7

Open the Home controller.

  • In the Solution Explorer.

  • Select the Controller folder then select "HomeController".

Enter this code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using ReteriveValue.Models;  
  7. namespace ReteriveValue.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         ReteriveDemoEntities reterive = new ReteriveDemoEntities();  
  12.         public ActionResult Index()  
  13.         {  
  14.             return View(reterive);  
  15.         }  
  16.     }  
  17. } 

 

Step 8

In the the "index.cshtml" file:

  • In the Solution Explorer.

  • Select the "View folder" then select "Home" -> "index.cshtml".

Write this code:

  1. @model ReteriveValue.Models.ReteriveDemoEntities  
  2. @{  
  3.    ViewBag.Title = "Index";  
  4. }  
  5. <h2>Reteriving Data From Database</h2>  
  6.  <!DOCTYPE html>  
  7. <html>  
  8. <head>  
  9.  <meta name="viewport" content="width=device-width" />  
  10. <title>Index</title>  
  11. <style type="text/css">  
  12. table, td, th {  
  13.  border: 1px solid green;  
  14. }  
  15.  th {  
  16.         background-color: Pink;  
  17.         color: white;  
  18.      }  
  19.     </style>  
  20.     </head>  
  21.     <body>  
  22.     <table style="margin-left: 25%; margin-top: 10px; border: 2px solid LightGray;">  
  23.      <tr>  
  24.          <th>ID</th>  
  25.          <th>Name</th>  
  26.          <th>Appointment</th>  
  27.          <th>Technology</th>  
  28.          <th>Task</th>  
  29.       </tr>  
  30.    @foreach (var item in Model.infoes)  
  31.    {  
  32.        <tr>  
  33.            <td>@item.ID</td>  
  34.            <td>@item.Name<td>  
  35.            <td>@item.Appointment<td>  
  36.            <td>@item.Technology</td>  
  37.            <td>@item.Task</td>  
  38.         </tr>  
  39.     }  
  40.  </table>  
  41.        </body>  
  42. </html> 

Step 9

Execute the application, the output will look like this:

re1.jpg