.NET 3.5 - DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                .NET 3.5 - DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            步步為營VS 2008 + .NET 3.5(8) - DLINQ(LINQ to SQL)之面向對象的添加、查詢、更新和刪除 
????????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>
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DAL;
public partial class LINQ_DLINQ_Sample : System.Web.UI.Page
{
????????// 實例化一個NorthwindDataContext(DataContext)
????????NorthwindDataContext _ctx = new NorthwindDataContext();
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????if (!Page.IsPostBack)
????????????????{
????????????????????????BindCategory();
????????????????}
????????}
????????private void BindCategory()
????????{
????????????????// NorthwindDataContext對象的Category屬性就是Category集合
????????????????var categories = _ctx.Categories;
????????????????gvCategory.DataSource = categories;
????????????????gvCategory.DataBind();
????????}
????????protected void btnAdd_Click(object sender, EventArgs e)
????????{
????????????????// 實例化一個Category
????????????????Categories c = new Categories();
????????????????// 設置Category對象的相關屬性
????????????????c.CategoryName = txtCategoryName.Text;
????????????????c.Description = txtDescription.Text;
????????????????// 使用NorthwindDataContext對象的InsertOnSubmit()方法添加Category對象
????????????????_ctx.Categories.InsertOnSubmit(c);
????????????????// 生成并執行相應的SQL命令
????????????????_ctx.SubmitChanges();
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
????????protected void gvCategory_SelectedIndexChanged(object sender, EventArgs e)
????????{
????????????????// 使用查詢語法獲得Product集合
????????????????var products = from p in _ctx.Products
???????????????????????????????????????????? where p.Categories.CategoryID == (int)gvCategory.SelectedValue
???????????????????????????????????????????? select p;
????????????????dvProduct.DataSource = products;
????????????????dvProduct.DataBind();
????????}
????????protected void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
????????{
????????????????// 使用Single查詢操作符獲取指定的Category對象
????????????????Categories category = _ctx.Categories.Single(c => c.CategoryID == (int)gvCategory.DataKeys[e.RowIndex].Value);
????????????????// 使用DeleteOnSubmit()方法刪除NorthwindDataContext對象的Category集合中的指定Category對象
????????????????_ctx.Categories.DeleteOnSubmit(category);
????????????????// 生成并執行相應的SQL命令
????????????????_ctx.SubmitChanges();
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
????????protected void gvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
????????{
????????????????// 使用查詢語法和Single查詢操作符獲取指定的Category對象
????????????????Categories category = (from c in _ctx.Categories
???????????????????????????????????????????????????????? where c.CategoryID == (int)gvCategory.DataKeys[e.RowIndex].Value
???????????????????????????????????????????????????????? select c).Single();
????????????????// 設置Category對象的相關屬性
????????????????category.CategoryName = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
????????????????category.Description = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
????????????????// 生成并執行相應的SQL命令
????????????????_ctx.SubmitChanges();
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
????????protected void gvCategory_RowEditing(object sender, GridViewEditEventArgs e)
????????{
????????????????gvCategory.EditIndex = e.NewEditIndex;
????????????????BindCategory();
????????}
????????protected void gvCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
????????{
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
}
OK
[源碼下載]
                        
                        
                        
作者:webabcd
介紹
以Northwind為示例數據庫,DLINQ(LINQ to SQL)之完全面向對象的添加操作、查詢操作、更新操作和刪除操作
示例
Sample.aspx
????????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 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DAL;
public partial class LINQ_DLINQ_Sample : System.Web.UI.Page
{
????????// 實例化一個NorthwindDataContext(DataContext)
????????NorthwindDataContext _ctx = new NorthwindDataContext();
????????protected void Page_Load(object sender, EventArgs e)
????????{
????????????????if (!Page.IsPostBack)
????????????????{
????????????????????????BindCategory();
????????????????}
????????}
????????private void BindCategory()
????????{
????????????????// NorthwindDataContext對象的Category屬性就是Category集合
????????????????var categories = _ctx.Categories;
????????????????gvCategory.DataSource = categories;
????????????????gvCategory.DataBind();
????????}
????????protected void btnAdd_Click(object sender, EventArgs e)
????????{
????????????????// 實例化一個Category
????????????????Categories c = new Categories();
????????????????// 設置Category對象的相關屬性
????????????????c.CategoryName = txtCategoryName.Text;
????????????????c.Description = txtDescription.Text;
????????????????// 使用NorthwindDataContext對象的InsertOnSubmit()方法添加Category對象
????????????????_ctx.Categories.InsertOnSubmit(c);
????????????????// 生成并執行相應的SQL命令
????????????????_ctx.SubmitChanges();
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
????????protected void gvCategory_SelectedIndexChanged(object sender, EventArgs e)
????????{
????????????????// 使用查詢語法獲得Product集合
????????????????var products = from p in _ctx.Products
???????????????????????????????????????????? where p.Categories.CategoryID == (int)gvCategory.SelectedValue
???????????????????????????????????????????? select p;
????????????????dvProduct.DataSource = products;
????????????????dvProduct.DataBind();
????????}
????????protected void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
????????{
????????????????// 使用Single查詢操作符獲取指定的Category對象
????????????????Categories category = _ctx.Categories.Single(c => c.CategoryID == (int)gvCategory.DataKeys[e.RowIndex].Value);
????????????????// 使用DeleteOnSubmit()方法刪除NorthwindDataContext對象的Category集合中的指定Category對象
????????????????_ctx.Categories.DeleteOnSubmit(category);
????????????????// 生成并執行相應的SQL命令
????????????????_ctx.SubmitChanges();
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
????????protected void gvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
????????{
????????????????// 使用查詢語法和Single查詢操作符獲取指定的Category對象
????????????????Categories category = (from c in _ctx.Categories
???????????????????????????????????????????????????????? where c.CategoryID == (int)gvCategory.DataKeys[e.RowIndex].Value
???????????????????????????????????????????????????????? select c).Single();
????????????????// 設置Category對象的相關屬性
????????????????category.CategoryName = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
????????????????category.Description = ((TextBox)gvCategory.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
????????????????// 生成并執行相應的SQL命令
????????????????_ctx.SubmitChanges();
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
????????protected void gvCategory_RowEditing(object sender, GridViewEditEventArgs e)
????????{
????????????????gvCategory.EditIndex = e.NewEditIndex;
????????????????BindCategory();
????????}
????????protected void gvCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
????????{
????????????????gvCategory.EditIndex = -1;
????????????????BindCategory();
????????}
}
OK
[源碼下載]
?
本文出自 “webabcd” 博客,請務必保留此出處http://webabcd.blog.51cto.com/1787395/345006
總結
以上是生活随笔為你收集整理的.NET 3.5 - DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 212
 - 下一篇: Word Clock数字时钟动态屏保,让