Linq to sql 增删改查(转帖)
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 增删改查(转帖)的更多相关文章
- linq to sql 增删改查
ORM<Object Relation Mapping> Linq To Sql: 一.建立Linq To Sql 类 : 理解上下文类: Linq To Sql 类名+context 利 ...
- linq的简单增删改查
Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(反正最后结果就是不用写ado.Net那一套增删改查,有一套封装好 ...
- [原创]Linq to xml增删改查Linq 入门篇:分分钟带你遨游Linq to xml的世界
本文原始作者博客 http://www.cnblogs.com/toutou Linq 入门篇(一):分分钟带你遨游linq to xml的世界 本文原创来自博客园 请叫我头头哥的博客, 请尊重版权, ...
- 表结构修改以及sql增删改查
修改表结构 修改表名 alter table 表名 rename 新名 增加字段 alter table 表名 add 字段名 数据类型 约束 删除字段 alter table 表名 drop 字段名 ...
- sql增删改查封装
App.config文件 <?xml version="1.0" encoding="utf-8" ?> <configuration> ...
- sql增删改查-转载
一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...
- SQL增删改查
1.增 INSERT INTO table_name VALUES (value1, value2,....) INSERT INTO table_name (列1, 列2,...) VALUES ( ...
- Linq to XML 增删改查
Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...
- SQL 增删改查(具体)
一.增:有3种方法 1.使用insert插入单行数据: insert [into] <表名> [列名] values <列值> insert into Strdents (na ...
随机推荐
- Oracle 使用GSON库解析复杂json串
在前文中讲到了如何使用JSON标准库解析json串,参考: Oracle解析复杂json的方法(转) 现补充一篇使用GSON库在Oracle中解析复杂json的方法. GSON串的使用教程参考官方文档 ...
- innodb_trx, innodb_locks, innodb_lock_waits
如果两个事务出现相互等待,则会导致死锁,MySQL的innodb_lock_wait_timeout参数设置了等待的时间限制,超时则抛异常. select @@innodb_lock_wait_tim ...
- es-hadoop saveToEsWithMeta
@Test def testEsRDDWriteWithDynamicMapping() { val doc1 = Map("one" -> null, "two& ...
- 线程同步,条件变量pthread_cond_wait
与互斥锁不同,条件变量是用来等待而不是用来上锁的.条件变量用来自动阻塞一个线程,直到某特殊情况发生为止.条件变量使我们可以睡眠等待某种条件出现.条件变量是利用线程间共享的全局变量进行同步的一种机制,主 ...
- 等待进程结束wait,waitpid
当子进程先于父进程退出时,如果父进程没有调用wait和waitpid函数,子进程就会进入僵死状态. pid_t wait(int *status); pid_t waitpid(pid_t pid, ...
- chrome plugins
ehpomnigmfglbkmnboidmmhhmicfdmom_1_1_0知行-时间管理 必开 Adkill and Media Download Cnblogs Wz(博客园网摘) Kami - ...
- PHP:第一章——php中数据类型和强制类型转换
<?php //PHP中的数据类型: //标量类型:布尔型(boolean).整型(integer).浮点型(float).字符串型(string) //复合类型:数组(array).对象(ob ...
- WEBSERVICE-CXF服务端代码
Spring + cxf 发布webservice 依赖的jar包 WEB.xml的配置 applicationContext.xml配置 部署在Tomcat中启动 出现的问题 ...
- hadoop kafka import/export data (8)
reference: http://kafka.apache.org/quickstart need to solve issue ISSUE 1: [2019-01-29 15:59:39,272] ...
- [转载]java中io流关闭的顺序
原文URL:http://blog.csdn.net/shijinupc/article/details/7191655 还是先看API void close() Closes ...