📜  LINQ to SQL CRUD操作

📅  最后修改于: 2021-01-06 06:00:06             🧑  作者: Mango

LINQ to SQL插入,删除和更新操作

LINQ to SQL具有维护更改的功能,无论我们对对象执行什么操作,例如添加,删除或更新对象集合中的项目,直到我们通过使用SubmitChanges()方法提交更改为止。提交更改后,LINQ to SQL将我们的操作转换为SQL并将更改提交到数据库。

LINQ to SQL插入/更新和删除的语法

这是在LINQ to SQL中使用的插入/更新或删除操作的语法。

LINQ to SQL插入操作

这是在C#中使用LINQ to SQL插入操作查询在数据库中插入数据的语法。

EmployeeDBDataContext db = new EmployeeDBDataContext();
EmployeeDetail emp = new EmployeeDetail();
emp.EmpName = txtname.Text;
emp.Location = txtlocation.Text;
db.EmployeeDetails.InsertOnSubmit(emp);
db.SubmitChanges();

LINQ to SQL更新操作

这是使用LINQ to SQL更新操作查询来更新数据库中数据的语法。

EmployeeDBDataContext db = new EmployeeDBDataContext();
EmployeeDetail emp = new EmployeeDetail();
emp = db.EmployeeDetails.Single(x => x.EmpId == empid);
emp.EmpName = txtname.Text;
emp.Location = txtlocation.Text;
emp.Gender = txtgender.Text;
db.SubmitChanges();

LINQ to SQL删除操作

这是在C#中使用LINQ to SQL删除操作删除数据库中数据的语法。

EmployeeDBDataContext db = new EmployeeDBDataContext();
EmployeeDetail emp = new EmployeeDetail();
emp = db.EmployeeDetails.Single(x => x.EmpId == empid);
db.EmployeeDetails.DeleteOnSubmit(emp);
db.SubmitChanges();

LINQ to SQL插入,更新,删除操作

在我们开始实施LINQ to SQL的插入,更新和删除操作之前,首先需要创建一个具有所需表的数据库,并将这些表映射到LINQ to SQL文件( .dbml )。

现在创建所需表并将其映射到.dbml文件后,我们想在应用程序中显示数据。为此,我们将右键单击应用程序->选择添加->新建项目->选择Web表单->将名称作为Default.aspx,然后单击确定按钮

现在打开Default.aspx页面并编写如下代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





    


    

现在打开文件后面的代码,并如下所示编写代码:

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

public partial class _Default : System.Web.UI.Page
{
    EmployeeDataContext db = new EmployeeDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)

        {

            BindGridview();

        }
    }
    protected void BindGridview()

    {

        var result = from ed in db.EmployeeDetails

                     select new

                     {

                         Id = ed.Id,

                         Name = ed.Name,

                         Location = ed.Location,

                         Gender = ed.Gender

                     };

        gvDetails.DataSource = result;

        gvDetails.DataBind();

    }
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)

    {

        if (e.CommandName.Equals("AddNew"))

        {

            TextBox txtname = (TextBox)gvDetails.FooterRow.FindControl("txtfName");

            TextBox txtlocation = (TextBox)gvDetails.FooterRow.FindControl("txtfLocation");

            TextBox txtgender = (TextBox)gvDetails.FooterRow.FindControl("txtfGender");

            EmployeeDetail emp = new EmployeeDetail();

            emp.Name = txtname.Text;

            emp.Location = txtlocation.Text;

            emp.Gender = txtgender.Text;

            db.EmployeeDetails.InsertOnSubmit(emp);

            db.SubmitChanges();

            lblresult.ForeColor = Color.Green;

            lblresult.Text = txtname.Text + " details inserted successfully";

            BindGridview();

        }

    }
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)

    {

        gvDetails.EditIndex = e.NewEditIndex;

        BindGridview();

    }

    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        gvDetails.EditIndex = -1;

        BindGridview();

    }

    protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        gvDetails.PageIndex = e.NewPageIndex;

        BindGridview();


    }
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        int empid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["Id"].ToString());

        TextBox txtname = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtName");

        TextBox txtlocation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtLocation");

        TextBox txtgender = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtGender");

        EmployeeDetail emp = new EmployeeDetail();

        emp = db.EmployeeDetails.Single(x => x.Id == empid);

        emp.Name = txtname.Text;

        emp.Location = txtlocation.Text;

        emp.Gender = txtgender.Text;

        db.SubmitChanges();



        gvDetails.EditIndex = -1;

        BindGridview();

        lblresult.ForeColor = Color.Green;

        lblresult.Text = txtname.Text + " details updated successfully";
    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        int empid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["Id"].ToString());

        string empname = gvDetails.DataKeys[e.RowIndex].Values["Name"].ToString();

        EmployeeDetail emp = new EmployeeDetail();

        emp = db.EmployeeDetails.Single(x => x.Id == empid);

        db.EmployeeDetails.DeleteOnSubmit(emp);

        db.SubmitChanges();

        BindGridview();

        lblresult.ForeColor = Color.Green;

        lblresult.Text = empname + " details deleted successfully";

    }
}

在上面的示例中,我们正在实现LINQ to SQL的选择,插入以及更新,删除操作。现在,我们将运行该应用程序并查看结果。