http://blog.csdn.net/pan_junbiao/article/details/7015633   (LINQ To SQL 语法及实例大全)

代码

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.Common;

using System.Collections.Generic;

namespace FirstLinq

{

public partial class _Default : System.Web.UI.Page

{

NorthwindDataContext ctx = new NorthwindDataContext("Data Source=ZHANGSHUQIANG;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa");

protected void Page_Load(object sender, EventArgs e)

{

Linq1();

Linq2();

Linq3();

}

/// <summary>

/// 增删改查

/// </summary>

private void Linq3()

{

//增

t_User user = new t_User();

user.UserName = "大气象";

user.Pwd = "123456";

ctx.t_User.InsertOnSubmit(user);//以前的方法是Add();

ctx.SubmitChanges();

//改

//参考这样的语法string s = (from a in ctx.Customers select a).Single(a => a.ID == 2);

t_User userUpdate = ctx.t_User.SingleOrDefault(t_User => t_User.ID == 2);//Single与SingleOrDefault没区别

userUpdate.UserName = "大气象1";

ctx.SubmitChanges();

//删

t_User userDelete = (from userinfo in ctx.t_User where userinfo.ID == 1 select userinfo).FirstOrDefault();

if (userDelete != null)

{

ctx.t_User.DeleteOnSubmit(userDelete);

ctx.SubmitChanges();

}

}

/// <summary>

/// 熟悉Linq to sql语法

/// </summary>

private void Linq2()

{

Customers customer = new Customers();

//执行普通的sql语句,查询CustomerID="ANATR"的记录

IEnumerable<Customers> customers = ctx.ExecuteQuery<Customers>("select * from Customers where CustomerID='ANATR'");

customer = customers.First();

Response.Write(customer.CustomerID);

//使用Linq查询单条记录

var cus = from c in ctx.Customers where c.CustomerID.Equals("ANATR") select c;

customer = cus.First();

Response.Write(customer.CompanyName);

//查询结果集,语法:from 临时表名 in 表集合 orderby 临时表名.字段名 升级序 select 临时表名

gdvCustomers.DataSource = from cust in ctx.Customers where cust.CustomerID != "ANATR" orderby cust.CompanyName descending select cust;

gdvCustomers.DataBind();

}

/// <summary>

/// 熟悉一下linq语法,变量的定义类似js,无须声明类型。

/// </summary>

private void Linq1()

{

var age = 29;

var username = "greatverve";

var userlist = new[] { "a", "b", "c" };

foreach (var user in userlist)

{

Response.Write(user);

}

}

}

}

http://www.cnblogs.com/greatverve/archive/2010/05/13/1734236.html

Linq to sql 增删改查(转帖)的更多相关文章

  1. linq to sql 增删改查

    ORM<Object Relation Mapping> Linq To Sql: 一.建立Linq To Sql 类 : 理解上下文类: Linq To Sql 类名+context 利 ...

  2. linq的简单增删改查

    Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...

  3. [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界

    本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...

  4. 表结构修改以及sql增删改查

    修改表结构 修改表名 alter table 表名 rename 新名 增加字段 alter table 表名 add 字段名 数据类型 约束 删除字段 alter table 表名 drop 字段名 ...

  5. sql增删改查封装

    App.config文件 <?xml version="1.0" encoding="utf-8" ?> <configuration> ...

  6. sql增删改查-转载

    一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...

  7. SQL增删改查

    1.增 INSERT INTO table_name VALUES (value1, value2,....) INSERT INTO table_name (列1, 列2,...) VALUES ( ...

  8. Linq to XML 增删改查

    Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...

  9. SQL 增删改查(具体)

    一.增:有3种方法 1.使用insert插入单行数据: insert [into] <表名> [列名] values <列值> insert into Strdents (na ...

随机推荐

  1. 使用XML Publisher导出PDF报表

    生成XML数据源有两种方式. 一种是使用存储过程,返回一个clob作为xml数据源. 另一种是直接使用VO中的数据生成xml数据源. 方法一参考: Oracle XML Publisher技巧集锦 O ...

  2. iOS UI-团购案例(通过xib文件自定义UITableViewCell)

    一.Model #import <Foundation/Foundation.h> @interface Goods : NSObject @property (nonatomic, co ...

  3. OC 设计模式

    设计模式 一种或几种被所有程序员广泛认同的,有某些特定功能,或者实现某些特殊作用的编码格式 单例模式 键值编码(KVC) 键值观察(KVO) 观察者模式() 工厂模式(工厂方法) ps:MVC &am ...

  4. BZOJ1907 树的路径覆盖

    ydc题解上写着贪心,后来又说是树形dp...可惜看不懂(顺便骗三连) 其实就是每个叶子开始拉一条链,从下面一路走上来,遇到能把两条链合起来的就合起来就好了. /******************* ...

  5. git commit steps(1)

    git commit steps: if you are a new git user , you may need to set # git config --global user.email & ...

  6. SQL Server 调优系列基础篇 - 子查询运算总结

    前言 前面我们的几篇文章介绍了一系列关于运算符的介绍,以及各个运算符的优化方式和技巧.其中涵盖:查看执行计划的方式.几种数据集常用的连接方式.联合运算符方式.并行运算符等一系列的我们常见的运算符.有兴 ...

  7. [转载]python的常用代码模板

    URL:http://blog.csdn.net/xingjiarong/article/details/50651235

  8. 链表(list)的实现(c语言)

    链表是一种基本的数据结构,今天练习了一下,所以将代码贴在下面,代码测试通过,代码还可以优化,我会过段时间就会增加一部分或者优化一部分直达代码无法优化为止,我的所有数据结构和算法都会用这样的方式在博客上 ...

  9. localstorage检测

    localstorage检测 初来乍到,刚刚接触一个从零开始的移动wap项目,希望内容根据策略(正在思考中)在浏览器中缓存,appcache.localstorage都作为泛化知识进行了解和练习,知道 ...

  10. 在junit中添加fail--有test失败即build Failed

    项目使用jenkins做持续集成,ant来构建,发现在跑junit单元测试的时候,如果有test case失败了,ci的状态是黄色的unstable,而不是红色的failed,看起来很不爽.个人觉得b ...