.NET 3.5 - DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除
作者: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>
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)之面向对象的添加、查询、更新和删除相关推荐
- .NET 3.5(12) - DLINQ(LINQ to SQL)之事务处理和并发处理
步步为营VS 2008 + .NET 3.5(12) - DLINQ(LINQ to SQL)之事务处理和并发处理 作者:webabcd 介绍 以Northwind为示例数据库,DLINQ(LINQ ...
- .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页.延迟执行和日志记录 作者:webabcd 介绍 以Northwind为示例数据库,DLIN ...
- ScottGu之博客翻译-LINQ to SQL第三部分,查询数据库 (Part 3 - Querying our Database)
本贴只为共享知识,更为简洁(即无英文的版本)将会发布在博客堂上,堂主正对此文进行审阅. 希望本贴能对您的LINQ to SQL语言的学习有一定的帮助! 原贴链接: http://weblogs.as ...
- sql server 快照_添加新文章,删除文章,更改快照文件夹路径和SQL Server复制中的数据筛选器行
sql server 快照 In the last articles, we have learned Configuring Snapshot and Transactional SQL Serve ...
- LINQ to SQL 运行时动态构建查询条件
原文地址:http://msdn.microsoft.com/zh-cn/dd567295.aspx 在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句 ...
- linq to sql的多条件动态查询(下)
借助老外写的一个扩展表达式的类,可以把上篇中的代码写得更优雅 这是PredicateBuilder的源文件 public static class PredicateBuilder ...{ ...
- 测试SqlHelp,linq to SQL,Nhibernate批量处理数据的效率 2009-06-07
(SqlHelp是一个操作数据库的辅助类,linq to SQL,Nhibernate,是数据持久化框架, linq to SQ只适合MS SQL,Nhibernate适合多种数据源) 数据的准备: ...
- 浅谈LINQ to SQL集成数据库语言优劣
Linq To SQL是Microsoft开发的针对解决data!=object问题的新技术.在笔者的一系列的文章中,对它已经做了大量的介绍.现在,笔者将从经验的角度,谈谈它的优劣. 1.LINQ t ...
- LINQ To SQL 语法及实例大全
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...
最新文章
- node mongoose_如何使用Express,Mongoose和Socket.io在Node.js中构建实时聊天应用程序
- MATLAB和Python读取wave文件的波形对比
- 新浪微博、腾讯微博、QQ空间、人人网、豆瓣 一键分享API
- Java中使用Socket实现服务器端和客户端通讯
- PowerDesigner表结构和字段大小写转换
- import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; 报错的解决方法
- web安全day24:一文读懂Linux系统安装软件的几种方法
- 【OpenCV教程】第一章 Mac上的安装和初步使用
- 华为NP课程笔记15-Eth-Trunk与高级VLAN
- 推荐几个e书下载地址
- 官方免费申请许可证-VMware Fusion 12 – Personal Use License
- javaw和java,java,javaw和javaws之间有什么区别?
- 【虫师--系列15】性能测试知多少---系统架构分析
- Photoshop抠图(磁性套索工具及几个常用案例)
- 《互联网时代》 第八集 忧虑
- opencv、opencv_contrib及cuda联合编译详细教程
- 高中计算机专业班主任工作总结,中等专业学校计算机班主任年工作总结
- 乐学偶得python视频_乐学偶得 - 课程
- 北京搬家货运公司汇总
- BSD License
热门文章
- jquery find 找到frame select_简述jQuery
- iphone怎样关闭副屏_iPhone手机关掉这3个设置,不仅省电,而且手机还不会卡
- 360网络修复工具_Win10网络图标不见了解决方法
- 光遇服务器维护都在干什么,光遇:全图毕业后该做什么?老玩家建议,这四件事不做会后悔...
- aspx,ascx和ashx使用小结
- 什么是基金净值、单位净值、累计净值
- 第二篇 python基础知识总结:数据、运算符
- Python: 编程遇到的一些问题以及网上解决办法?
- 使用junit测试用例
- STL vector容器