Linq To Sql

How to Use LINQ to SQL?

  • Step 1: Make a new “Data Connection” with database server. View -> Server Explorer -> Data Connections -> Add Connection
  • Step 2: Add LINQ To SQL class file.
  • Step 3: Select tables from database and drag and drop into the new LINQ to SQL class file.
  • Step 4: Added tables to class file.

CRUD Operation using LINQ to SQL?

Design
CRUD OPERATION
Curd_Operation.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Curd_Operation : System.Web.UI.Page
{
    DataClassesDataContext dc = null;

    public Curd_Operation()
    {
        dc = new DataClassesDataContext();
    }

     private void ShowEmp()
    {
        var data = dc.tblEmployees.ToList();
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        ShowEmp();
    }
 
    protected void btnInsert_Click1(object sender, EventArgs e)
    {
        tblEmployee empobj = new tblEmployee();
        empobj.EmpName = txtEmpName.Text;
        empobj.EmpSalary = Convert.ToInt64(txtEmpSalary.Text);
        empobj.EmpDoj = txtEmpDoj.Text;

        dc.tblEmployees.InsertOnSubmit(empobj);
        dc.SubmitChanges();
        ShowEmp();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {    
        tblEmployee empobj = dc.tblEmployees.Where(eid => eid.pkEmpId == Convert.ToInt32(txtEmpId.Text)).Single();
        empobj.EmpName = txtEmpName.Text;
        empobj.EmpSalary = Convert.ToInt64(txtEmpSalary.Text);
        empobj.EmpDoj = txtEmpDoj.Text;
        dc.SubmitChanges();
        ShowEmp();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        tblEmployee empobj = dc.tblEmployees.Where(eid => eid.pkEmpId == Convert.ToInt32(txtEmpId.Text)).Single();
        dc.tblEmployees.DeleteOnSubmit(empobj);
        dc.SubmitChanges();
        ShowEmp();
    }
}

No comments:

Post a Comment

Xamarin Android Project App

Xamarin Android Project App 1. Xamarin -- Make a CALCULATOR Android App   https://drive.google.com/open?id=0B8OPCXLrtKPmWC1FWWtFM2lraVk...