LinqToSQL实例参见
/// <summary>
/// (增)向表中插入数据
/// </summary>
public void InsertData()
{
LinqToSQLDataContext dataContent = new LinqToSQLDataContext();
//InsertOnSubmit(TEntity entity)方法向表一次插入一条记录
employee e = new employee() { id = 4, name = "sujina", age = 22, position = "教师", partment = "狮子场小学" };
dataContent.employee.InsertOnSubmit(e);
//InsertAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) 向表中一次插入多条记录
employee e1 = new employee() { id = 5, name = "sujina", age = 22, position = "教师", partment = "狮子场小学" };
employee e2 = new employee() { id = 6, name = "sujina", age = 22, position = "教师", partment = "狮子场小学" };
List<employee> epList = new List<employee>() { e1, e2 };
dataContent.employee.InsertAllOnSubmit(epList);
dataContent.SubmitChanges();//计算要插入、更新或删除的已修改对象的集,并执行相应的命令以实现对数据库的更改
}
/// <summary>
/// (删)删除数据
/// </summary>
public void deleteData()
{
LinqToSQLDataContext dataContent = new LinqToSQLDataContext();
//删除一个实体
//Single<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)返回序列中满足指定条件的唯一元素;如果有多个这样的元素存在则会报错
employee deletes = dataContent.employee.Single(x => x.id == 4);
dataContent.employee.DeleteOnSubmit(deletes);
//删除多个实体
IEnumerable<employee> delete = dataContent.employee.Where(x => x.id == 2).Select(x => x);//兰姆达表达式
//IEnumerable<employee> delete = from s in dataContent.employee where s.id == 2 select s;//linq写法
dataContent.employee.DeleteAllOnSubmit(delete);
dataContent.SubmitChanges();
}
/// <summary>
/// (改)更新数据
/// </summary>
public void updteData()
{
LinqToSQLDataContext dataContent = new LinqToSQLDataContext();
//查出多条数据
IEnumerable<employee> update = dataContent.employee.Where(x => x.id > 4).Select(x => x);//IEnumerable<employee>类型
//IEnumerable<employee> update = from s in dataContent.employee where s.id >4 select s;//IEnumerable<employee>类型
foreach (var item in update)
{
item.name = "zybsjn";
item.partment = "酒店学校";
item.position = "教师程序员";
}
//查出单条数据
employee ep = dataContent.employee.Single(x => x.id == 5);
ep.name = "小娜娜";
dataContent.SubmitChanges();
}
LinqToSQL实例参见的更多相关文章
- CGroup 介绍、应用实例及原理描述
CGroup 介绍 CGroup 是 Control Groups 的缩写,是 Linux 内核提供的一种可以限制.记录.隔离进程组 (process groups) 所使用的物力资源 (如 cpu ...
- Greenwich.SR2版本的Spring Cloud Ribbon实例
上次我们了解了eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例),里面的服务消费方(服务实例a-beautiful-client)我们其实已经用到了ribb ...
- SPRING SECURITY JAVA配置:Web Security
在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...
- 【转】libevent源码分析
libevent源码分析 转自:http://www.cnblogs.com/hustcat/archive/2010/08/31/1814022.html 这两天没事,看了一下Memcached和l ...
- JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)
参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...
- windows批处理
1.日期作为变量当做文件名的一部分. C:\Documents and Settings\Simon>echo %date%2008-09-09 星期二 C:\Documents and Set ...
- How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新)
How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新) 重要通知: 最近开题报告已差不多告一段落, ...
- JavaScript 闭包整合
初遇闭包感觉很困惑,上网查看了些许介绍,有很多没看懂,就想先对能懂的东西整整 首先觉得要了解闭包,要先对一.JavaScript的变量作用域和作用域链有基本了解 1.变量的作用域分为:全局变量和局部变 ...
- “内部类” 大总结(Java)
(本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...
随机推荐
- table的样式定义
table的cellpadding和cellpadding我们经常会用如下的方式来清除默认样式: <table cellspacing="0" cellpadding=&qu ...
- NFC协议学习分享
很多同学在学习NFC协议的时候,觉得NFC的规范从底层到上层的应有尽有,有点无处下手的感觉.这里就和大家分享下我曾经学习NFC规范的经验.如果有不对的地方,也请各位同学批评指正.NFC Forum中有 ...
- python图片小爬虫
import re import urllib import os def rename(name): name = name + '.jpg' return name def getHtml(url ...
- Linux常用C函数---字符串转换篇
函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/ atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表 ...
- error C2065: 'assert' : undeclared identifier
F:\VC6.0 : error C2065: 'assert' : undeclared identifier 导入#include <assert.h>
- 2014/4月金山WPS笔试
今晚去參加了金山的笔试. 一開始还以为选C++的人不会非常多. 我去啊,一去到,好多人,一整个大教室都快满人了. 还好我算是去的比較早的了. 还拿到了一个位置. 金山还是挺不错的,对于我这类还没有实力 ...
- how to translate the text of push button
Background:In a project, the need to translate the buttons on the screen, as shown below,the followi ...
- js(1)不太注意的一些小事件
确认选择 除了向用户提供信息,我们还希望从用户那里获得信息.这里就用到了confirm 消息对话框. 语法:confirm(str); 参数说明: str: 在消息对话框中要显示的文本,返回的是布尔值 ...
- HibernateTemplate 常用方法
HibernateTemplate 提供非常多的常用方法来完成基本的操作,比如通常的增加.删除.修改.查询等操作,Spring2.0更增加对命名SQL查询的支持,也增加对分页的支 持.大部分情况下,使 ...
- spring的官方文真不错
引文不太好,但是通过别的版本的查看,发现spring的文档真心不错,内容详细明了. http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-fra ...