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. maven分别打包开发、生产配置文件

    项目工程针对开发和生产有两套配置,开发配置文件目录:src/main/resources/    applicationContext.xml (开发和生产共用)    dubbo.propertie ...

  2. keras-anomaly-detection 代码分析——本质上就是SAE、LSTM时间序列预测

    keras-anomaly-detection Anomaly detection implemented in Keras The source codes of the recurrent, co ...

  3. 【webpack系列】1 What is webpack?

    什么是webpack? 现今的网页可以看做是功能丰富的应用,拥有着复杂的js代码和一大堆依赖包.为了简化开发的复杂程度,有了很多好用的实践方法 模块化 让我们可以把复杂的程序细化为小的文件 类似于Ty ...

  4. mysql 聚合函数

    1.sum 用法 有这种类型的数据: id date user_id result 1 2015-05-04 1 win 2 2015-05-06 1 loss 3 2015-05-09 2 loss ...

  5. Bootstrap CustomBox 弹层

    这个模态窗口插件使用原生javascript制作,它也可以和jQuery完美的结合.请注意:这些模态窗口动画仅仅工作在支持各自CSS3属性的浏览器上.Internet Explorer 8 和 9需要 ...

  6. python两个子线程通过queue通信

    SocketServer端代码 #!/usr/bin/env python import threading import SocketServer import time import queue ...

  7. ionic 编写自定义控件

    创建组件 在项目所在目录下执行: ionic g component <ComponentName> 在src/components中会出现: --components |--Compon ...

  8. (转)2017年12月宋华教授携IBM中国研究院、猪八戒网、中航信托、33复杂美共同论道智慧供应链金融

    今年10月,国务院发布的<关于积极推进供应链创新与应用的指导意见>,不仅强调了发展服务型制造的重要性,同时也指出要积极稳妥发展供应链金融,推动供应链金融服务实体经济,鼓励商业银行.供应链核 ...

  9. QNX的深度嵌入过程

    QNX的深度嵌入过程   1.1           QNX目标系统嵌入 利用QNX的模块性和和可裁剪性,其嵌入过程一般是: 构建Buildfile -> 编译buildfile生成系统映象文件 ...

  10. ScrollView在布局中的作用

    ScrollView就是滚动一个View,将View里面的内容滚动起来. 但是由于scroolview只能有一个孩子,因此我们可以在ScrollView中在定义一个布局. 这样的话,我们就会直接滚动整 ...