Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Linq query for Database Operation using ADO.NET Entity Framework:-

Linq means Language Integrated Query, It is a modified form of SQL query, LINQ provides database query using database object, it will treat table component as a model object to perform database operation

LINQ is an application-based query language that means we can write queries using c# code.

.NET provides an ADO.NET Entity Framework to manage database operation using NOSQL. in place of SQL, it uses LINQ Query.

LINQ performance and security as best compare to SQL query hence for modern application development, we always prefer LINQ query to perform database operation.


How to work with Database:-

1)  Right-click on project or APP_Data and create a database.

2) Right-click on the database and open the database.

3)  Create a table using a set of columns


4)  Add data to the table

5)  Right-click on models and  add new item --->  Data ---->  Ado.NET Entity Data Model -=--> next ---> Generate from database


Complete Code of Controller:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QMS.Models;
namespace QMS.Controllers
{
    public class HomeController: Controller
    {
        Database1Entities db = new Database1Entities();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]

        public ActionResult Index(Registration r)
        {
            var s = (from c in db.Registrations where c.userid == r.userid && c.password == r.password select c).FirstOrDefault();
            if (s != null)
            {
                ViewBag.data = "Login Successfully";
            }
            else
            {
                ViewBag.data = "Invalid Userid and Password";
            }

            return View();
        }

}



Complete Code of View

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/shared/_MasterPage.cshtml";
}

@model QMS.Models.Registration
<p>


    <h1>Welcome in QMS System</h1>
    <h2>"Using this software, you can improvide quality index of company"</h2>

    <center>
    @using (Html.BeginForm())
    {

        <p>Enter Username</p>
        <p>@Html.TextBoxFor(a=>a.userid) </p>
         <p>Enter Password</p>
        <p>@Html.TextBoxFor(a=>a.password) </p>
        
        <p><input type="submit" name="btnsubmit" value="Login" </p>
        
    }

        @ViewBag.data
        </center>
</p>





Select Update, Delete Example:-

1)  Complete Code of Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QMS.Models;
using System.Data;
namespace QMS.Controllers
{
    public class DashboardController : Controller
    {
        //
        // GET: /Dashboard/
        Database1Entities db = new Database1Entities();
        public ActionResult Index()
        {
           // var s = from c in db.Registrations select c;
             var s =  db.Registrations.ToList();
           // return View(s.ToList());
             return View(s);
        }

        public ActionResult Edit(String id)
        {
         //   var s = from c in db.Registrations where c.userid == id select c;
            var s = db.Registrations.Find(id);
            return View(s);
        }
        [HttpPost]
        public ActionResult Edit(Registration r)
        {
            db.Entry(r).State = EntityState.Modified;   // ref is the reference of Model Class Object
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Details(String id)
        {
            var s = db.Registrations.Find(id);
            return View(s);
        }
        public ActionResult Delete(String id)
        {
            //   var s = from c in db.Registrations where c.userid == id select c;
            var s = db.Registrations.Find(id);
            return View(s);
        }
        [HttpPost]
        public ActionResult DeleteConfirmed(String userid)
        {
            var s= db.Registrations.Find(userid);
            db.Registrations.Remove(s);   //ref is the reference of Model Class Object
            db.SaveChanges();

            return RedirectToAction("Index");
        }
    }
}


2)  Code of index.cshtml

@model IEnumerable<QMS.Models.Registration>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.userid)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.password)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.role)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.userid)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.password)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.role)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.userid }) |
                @Html.ActionLink("Details", "Details", new { id=item.userid}) |
                @Html.ActionLink("Delete", "Delete", new { id=item.userid})
            </td>
        </tr>
    }
    
    </table>
</body>
</html>


3)  Code of Edit.cshtml

@model QMS.Models.Registration

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>Registration</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.userid)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.userid)
                @Html.ValidationMessageFor(model => model.userid)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.password)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.password)
                @Html.ValidationMessageFor(model => model.password)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.role)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.role)
                @Html.ValidationMessageFor(model => model.role)
            </div>
    
            <p>
                <input type="submit" value="Update" />
            </p>
        </fieldset>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>

4)  details.html

    @model QMS.Models.Registration

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Details</title>
</head>
<body>
    <fieldset>
        <legend>Registration</legend>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.userid)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.userid)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.password)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.password)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.role)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.role)
        </div>
    </fieldset>
    <p>
        @Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
</body>
</html>


5)  Code of delete.html

@model QMS.Models.Registration

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Delete</title>
</head>
<body>
    <h3>Are you sure you want to delete this?</h3>
    <fieldset>
        <legend>Registration</legend>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.userid)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.userid)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.password)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.password)
        </div>
    
        <div class="display-label">
             @Html.DisplayNameFor(model => model.role)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.role)
        </div>
    </fieldset>
    @using (Html.BeginForm("DeleteConfirmed","Dashboard")) {
        <p>
           @Html.HiddenFor(a=>a.userid)
            <input type="submit" value="Delete" /> |
            @Html.ActionLink("Back to List", "Index")
        </p>
    }

</body>
</html>



1)  The query for Data Selection:-

       var variablename =  from c in db.modelclassname select c    in SQL  (select * from tablename)

        c is the identifier, the model name will be in the plural name of the table

        var variablename = db.tablename()

     
        Select a Particular Column or Projection LINQ Query

        var data = from c in db  select new {column1 = c.columnname,column2=c.columnname }

       var data = from c in db.Students select new {rno=c.rno,sname=c.sname }

       IN SQL this query will be select rno,sname from Student


2)  Query for Data Selection based on Condition:-

    2.1)  var s = from c in db.tablename where c.columnname==value select c;
   
     in SQL this query will be select * from tablename where c.columnname=value

  2.2)
    var s = from c in db.tablename where c.columnname==value && c.columnname2==value Select c;
     in SQL this query will be  select * from tablename where c.columnname1=value and                  c.columnname2=value

2.3)
    var s = from c in db.tablename where c.columnname==value || c.columnname2==value select c;

in SQL query this will be Select *  from tablename where columnname1=value or columnname2=value


3)  For Data Insertion:-
                                                //DB is the Object of the database entity class
      db.tablename.Add(ref);   //  it is the reference of Model Class
      db.SaveChanges()    // It is used to reflect data of an application to a database server.

4)  For Data Updation:- 


    db.Entry(ref).State = EnityState.Modified;   // ref is the reference of Model Class Object
    db.SaveChanges()

5)   For Data Deletion:-

      db.tablename.Remove(ref);   //ref is the reference of Model Class Object
      db.SaveChanges()

 6)  For Find Record:-

       var variablename = db.tablename.Find(ref);
   


   




Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post