LINQ to SQL之面向对象的添加、查询、更新和删除

30 篇文章 0 订阅

http://www.prg-cn.com/article-4418-1.html

介绍

以Northwind为示例数据库,DLINQ(LINQ to SQL)之完全面向对象的添加操作、查询操作、更新操作和删除操作

示例

  1. Sample.aspx
  2. <%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile=
  3.     "Sample.aspx.cs"
  4.    Inherits="LINQ_DLINQ_Sample" Title="面向对象的添加、查 询、更新和删除" %>
  5. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
  6. </asp:Content>
  7. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
  8.   <p>
  9.     分类名称 :<asp:TextBox ID="txtCategoryName" runat="server"></asp:TextBox>
  10.       分类 描述:<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
  11.      <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
  12.    </p>
  13.   <asp:GridView ID="gvCategory" runat="server" DataKeyNames="CategoryID"
  14.          OnSelectedIndexChanged="gvCategory_SelectedIndexChanged"     OnRowDeleting="gvCategory_RowDeleting" OnRowCancelingEdit="gvCategory_RowCancelingEdit"
  15.      OnRowEditing="gvCategory_RowEditing" OnRowUpdating="gvCategory_RowUpdating">
  16.      <Columns>
  17.       <asp:CommandField ShowSelectButton="True" ShowEditButton="True"
  18.           ShowDeleteButton="True">
  19.        </asp:CommandField>
  20.     </Columns>
  21.    </asp:GridView>
  22.   <br />
  23.    <asp:DetailsView ID="dvProduct" runat="server" DataKeyNames="ProductID">
  24.    </asp:DetailsView>
  25. </asp:Content>
复制代码

Sample.aspx.cs

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Xml.Linq;
  13. using DAL;
  14. public partial class LINQ_DLINQ_Sample : System.Web.UI.Page
  15. {
  16.   // 实例化一个 NorthwindDataContext(DataContext)
  17.   NorthwindDataContext _ctx = new NorthwindDataContext();
  18.   protected void Page_Load (object sender, EventArgs e)
  19.   {
  20.     if (! Page.IsPostBack)
  21.     {
  22.       BindCategory();
  23.     }
  24.   }
  25.   private void BindCategory()
  26.   {
  27.     // NorthwindDataContext对象的Category属性就是 Category集合
  28.     var categories = _ctx.Categories;
  29.     gvCategory.DataSource = categories;
  30.      gvCategory.DataBind();
  31.   }
  32.   protected void btnAdd_Click(object sender, EventArgs e)
  33.   {
  34.     // 实 例化一个Category
  35.     Categories c = new Categories();
  36.     // 设置Category对象的相关属性
  37.      c.CategoryName = txtCategoryName.Text;
  38.     c.Description = txtDescription.Text;
  39.     // 使用NorthwindDataContext对 象的InsertOnSubmit()方法添加Category对象
  40.      _ctx.Categories.InsertOnSubmit(c);
  41.     // 生成并执行相 应的SQL命令
  42.     _ctx.SubmitChanges();
  43.      gvCategory.EditIndex = -1;
  44.     BindCategory();
  45.   }
  46.   protected void gvCategory_SelectedIndexChanged(object sender, EventArgs e)
  47.   {
  48.     // 使用查询语法获得 Product集合
  49.     var products = from p in _ctx.Products
  50.             where p.Categories.CategoryID == (int) gvCategory.SelectedValue
  51.             select p;
  52.     dvProduct.DataSource = products;
  53.      dvProduct.DataBind();
  54.   }
  55.   protected void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
  56.   {
  57.     // 使用Single查询操作符获取指定的Category对象
  58.     Categories category = _ctx.Categories.Single(c => c.CategoryID == (int)gvCategory.DataKeys[e.RowIndex].Value);
  59.     // 使用DeleteOnSubmit()方法删除NorthwindDataContext对象的 Category集合中的指定Category对象
  60.      _ctx.Categories.DeleteOnSubmit(category);
  61.     // 生成并 执行相应的SQL命令
  62.     _ctx.SubmitChanges();
  63.      gvCategory.EditIndex = -1;
  64.     BindCategory();
  65.   }
  66.   protected void gvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
  67.   {
  68.     // 使用查询语法和 Single查询操作符获取指定的Category对象
  69.     Categories category = (from c in _ctx.Categories
  70.                 where c.CategoryID == (int)gvCategory.DataKeys[e.RowIndex].Value
  71.                select c).Single();
  72.      // 设置Category对象的相关属性
  73.     category.CategoryName = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[2].
  74.          Controls[0]).Text;
  75.     category.Description = ((TextBox) gvCategory.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
  76.      // 生成并执行相应的SQL命令
  77.     _ctx.SubmitChanges();
  78.     gvCategory.EditIndex = -1;
  79.     BindCategory ();
  80.   }
  81.   protected void gvCategory_RowEditing (object sender, GridViewEditEventArgs e)
  82.   {
  83.      gvCategory.EditIndex = e.NewEditIndex;
  84.     BindCategory();
  85.   }
  86.   protected void gvCategory_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
  87.   {
  88.      gvCategory.EditIndex = -1;
  89.     BindCategory();
  90.   }
  91. }
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值