[索引页]
[×××]


步步为营VS 2008 + .NET 3.5(8) - DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除


作者: webabcd


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


示例
Sample.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Sample.aspx.cs"
        Inherits="LINQ_DLINQ_Sample" Title="面向对象的添加、查询、更新和删除" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <p>
                分类名称:<asp:TextBox ID="txtCategoryName" runat="server"></asp:TextBox>
                   分类描述:<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
                  
                <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
        </p>
        <asp:GridView ID="gvCategory" runat="server" DataKeyNames="CategoryID" OnSelectedIndexChanged="gvCategory_SelectedIndexChanged"
                OnRowDeleting="gvCategory_RowDeleting" OnRowCancelingEdit="gvCategory_RowCancelingEdit"
                OnRowEditing="gvCategory_RowEditing" OnRowUpdating="gvCategory_RowUpdating">
                <Columns>
                        <asp:CommandField ShowSelectButton="True" ShowEditButton="True" ShowDeleteButton="True">
                        </asp:CommandField>
                </Columns>
        </asp:GridView>
        <br />
        <asp:DetailsView ID="dvProduct" runat="server" DataKeyNames="ProductID">
        </asp:DetailsView>
</asp:Content>
Sample.aspx.cs
InBlock.gif using System;
InBlock.gif using System.Data;
InBlock.gif using System.Configuration;
InBlock.gif using System.Collections;
InBlock.gif using System.Linq;
InBlock.gif using System.Web;
InBlock.gif using System.Web.Security;
InBlock.gif using System.Web.UI;
InBlock.gif using System.Web.UI.WebControls;
InBlock.gif using System.Web.UI.WebControls.WebParts;
InBlock.gif using System.Web.UI.HtmlControls;
InBlock.gif using System.Xml.Linq;
InBlock.gif
InBlock.gif using DAL;
InBlock.gif
InBlock.gif public partial class LINQ_DLINQ_Sample : System.Web.UI.Page
InBlock.gif{
InBlock.gif         // 实例化一个NorthwindDataContext(DataContext)
InBlock.gif        NorthwindDataContext _ctx = new NorthwindDataContext();
InBlock.gif
InBlock.gif         protected void Page_Load( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 if (!Page.IsPostBack)
InBlock.gif                {
InBlock.gif                        BindCategory();
InBlock.gif                }
InBlock.gif        }
InBlock.gif
InBlock.gif         private void BindCategory()
InBlock.gif        {
InBlock.gif                 // NorthwindDataContext对象的Category属性就是Category集合
InBlock.gif                var categories = _ctx.Categories;
InBlock.gif
InBlock.gif                gvCategory.DataSource = categories;
InBlock.gif                gvCategory.DataBind();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void btnAdd_Click( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 // 实例化一个Category
InBlock.gif                Categories c = new Categories();
InBlock.gif
InBlock.gif                 // 设置Category对象的相关属性
InBlock.gif                c.CategoryName = txtCategoryName.Text;
InBlock.gif                c.Description = txtDescription.Text;
InBlock.gif
InBlock.gif                 // 使用NorthwindDataContext对象的InsertOnSubmit()方法添加Category对象
InBlock.gif                _ctx.Categories.InsertOnSubmit(c);
InBlock.gif
InBlock.gif                 // 生成并执行相应的SQL命令
InBlock.gif                _ctx.SubmitChanges();
InBlock.gif
InBlock.gif                gvCategory.EditIndex = -1;
InBlock.gif                BindCategory();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvCategory_SelectedIndexChanged( object sender, EventArgs e)
InBlock.gif        {
InBlock.gif                 // 使用查询语法获得Product集合
InBlock.gif                var products = from p in _ctx.Products
InBlock.gif                                             where p.Categories.CategoryID == ( int)gvCategory.SelectedValue
InBlock.gif                                             select p;
InBlock.gif
InBlock.gif                dvProduct.DataSource = products;
InBlock.gif                dvProduct.DataBind();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvCategory_RowDeleting( object sender, GridViewDeleteEventArgs e)
InBlock.gif        {
InBlock.gif                 // 使用Single查询操作符获取指定的Category对象
InBlock.gif                Categories category = _ctx.Categories.Single(c => c.CategoryID == ( int)gvCategory.DataKeys[e.RowIndex].Value);
InBlock.gif
InBlock.gif                 // 使用DeleteOnSubmit()方法删除NorthwindDataContext对象的Category集合中的指定Category对象
InBlock.gif                _ctx.Categories.DeleteOnSubmit(category);
InBlock.gif
InBlock.gif                 // 生成并执行相应的SQL命令
InBlock.gif                _ctx.SubmitChanges();
InBlock.gif
InBlock.gif                gvCategory.EditIndex = -1;
InBlock.gif                BindCategory();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvCategory_RowUpdating( object sender, GridViewUpdateEventArgs e)
InBlock.gif        {
InBlock.gif                 // 使用查询语法和Single查询操作符获取指定的Category对象
InBlock.gif                Categories category = (from c in _ctx.Categories
InBlock.gif                                                         where c.CategoryID == ( int)gvCategory.DataKeys[e.RowIndex].Value
InBlock.gif                                                         select c).Single();
InBlock.gif
InBlock.gif                 // 设置Category对象的相关属性
InBlock.gif                category.CategoryName = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
InBlock.gif                category.Description = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
InBlock.gif
InBlock.gif                 // 生成并执行相应的SQL命令
InBlock.gif                _ctx.SubmitChanges();
InBlock.gif
InBlock.gif                gvCategory.EditIndex = -1;
InBlock.gif                BindCategory();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvCategory_RowEditing( object sender, GridViewEditEventArgs e)
InBlock.gif        {
InBlock.gif                gvCategory.EditIndex = e.NewEditIndex;
InBlock.gif                BindCategory();
InBlock.gif        }
InBlock.gif
InBlock.gif         protected void gvCategory_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
InBlock.gif        {
InBlock.gif                gvCategory.EditIndex = -1;
InBlock.gif                BindCategory();
InBlock.gif        }
InBlock.gif}