译:在C#中使用LINQ To SQL
译文出处:http://www.codeproject.com/Tips/871938/LINQ-To-SQL-Using-Csharp
今天在这个话题中,我给大家分享一个在c#编程中非常有趣和十分有用的特性。
开始之前,我想告诉大家关于Linq的基本信息。比如:什么是linq?然后再来分享实际应用。
说明:
LINQ = Language Integrated Query(集成查询语言)
Linq是微软在.NET Framework 3.5中信增加的一个特性。它是用来查询数据库和对数据库查询的本地集合带来安全性。它非常简单但是很有组织性。一个普通的查询语言,适用于SQL, XML, 本地collections 和 第三方APIs 比如SharePoint.
本质上,Linq提供的就是一个轻量级的编程数据集成。这是非常有价值的,尤其是当今每天面对的数据和未来的大数据。
接下来我们就看看这个神秘的东东。
第一步:
- 打开你的Visual Studio 并且创建一个新的控制台项目.
- 然后打开服务器资源管理,创建一个新的数据库,新增一张表增加几个字段。
- 打开你的项目的解决方案目录,右击工程点击添加新增项。
- 查找LINQ-To-SQL 并添加。
- 你会看到一个空白的文件,在这个文件中你可以拖动你的表放在 LINQ-To-SQL .dbml 文件扩展上.
第二步:
我将声明一些类,添加一些类成员。
class Program
{ // this is program class
private int id;
private string name;
private string fname;
private int age;
private string sem;
}
第三步:
这里我将告诉大家如何通过 LINQ-To-SQL 向数据库中插入数据。
public void insert()
{
// these are the class data members through we will send our objects data in the database
Console.WriteLine("Enter id");
id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter name");
name = Console.ReadLine();
Console.WriteLine("Enter father name");
fname = Console.ReadLine();
Console.WriteLine("Enter age");
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter semester");
sem = Console.ReadLine();
// this is the data context class the main class which handles
// all the functionality in this will pass the connection string of our database file.
LTSDataContext LTS = new LTSDataContext
(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
// this is the table class which we drag on our linq to sql file
Student objStudentTable = new Student();
objStudentTable.Id = id;
objStudentTable.Name = name;
objStudentTable.Father_Name = fname;
objStudentTable.Age = age;
objStudentTable.Semester = sem;
LTS.Students.InsertOnSubmit(objStudentTable); // this is built in function.
LTS.SubmitChanges();// here is the final query will run in the data context class.
}
第四步:展示数据
void Display()
{
LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
var selectQuery = from s in LTS.Students
select s;
foreach (Student s in selectQuery)
{
Console.WriteLine(s.Id + "\t" + s.Name + "\t" +
s.Father_Name + "\t" + s.Age + "\t" + s.Semester);
}
}
第五步:删除数据
void Delete()
{
int iid = ;
Console.WriteLine("Enter the Id of the student u want to delete?");
iid = Convert.ToInt32(Console.ReadLine()); LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
var delete = from p in LTS.Students
where p.Id == iid
select p;
LTS.Students.DeleteAllOnSubmit(delete);
LTS.SubmitChanges();
Student objStudentTable = LTS.Students.Single(c=> c.Id == iid);
}
第六步:更新数据
void update()
{
LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
Integrated Security=True;Connect Timeout=30");
Student objStudentTable = new Student();
int iid = ;
Console.WriteLine("Enter the Id of the student u want to update ?");
iid = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the new name of the student u want to update?");
string up = (Console.ReadLine());
var update = from s1 in LTS.Students
where s1.Id == iid
select s1;
foreach (var v in update)
v.Name = up;
LTS.SubmitChanges();
}
主函数:
static void Main(string[] arg){ Program p1 = new Program(); // creates object
p1.insert();
p1.Display();
p1.Delete();
p1.update();
Console.ReadKey();
}
感谢您的阅读,请留下您的足迹。
Cheers! Enjoy coding.
译:在C#中使用LINQ To SQL的更多相关文章
- VB.NET中使用Linq TO SQL添加数据后获得自增长列ID
VB.NET中使用Linq TO SQL添加数据后获得自增长列ID: Dim tempOrdre As New Order With { .CustomerID = cmbCustomerName.S ...
- [转载代码]VB.NET 中查询 Linq to SQL 执行时的SQL语句
在搜索使用LINQ TO SQL 添加数据后获得自增长ID的方法时,发现C#可以使用DebuggerWritter把使用Linq to SQL执行的SQL语句显示到即时窗口,于是在网上搜索到在VB.N ...
- wcf+linq to sql中关联查询返回数据问题
前段时间准备采用wcf+nh框架开发sl程序,发现采用nh开发不适合我的中型.并且快速开发项目,所以综合考量了下,决定采用wcf+linq to sql . 但是此模式也有缺点,也是linq to s ...
- linq to sql 中增删改查
首先我先说一下,如果真的要用linq做项目的话,也会是比较方便的.已经尝试了在三层架构中应用linq to sql 比较方便. //有三个不同的数据库表,所以写法不一样 public class Li ...
- 年终巨献 史上最全 ——LINQ to SQL语句
LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...
- LINQ to SQL语句(18)之运算符转换
运算符转换 1.AsEnumerable:将类型转换为泛型 IEnumerable 使用 AsEnumerable<TSource> 可返回类型化为泛型 IEnumerable 的参数.在 ...
- LINQ to SQL语句(13)之开放式并发控制和事务
Simultaneous Changes开放式并发控制 下表介绍 LINQ to SQL 文档中涉及开放式并发的术语: 术语 说明 并发 两个或更多用户同时尝试更新同一数据库行的情形. 并发冲突 两个 ...
- LINQ TO SQL 大全
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 LINQ to SQL语句(1)之Where 适用场景: ...
- LINQ to SQL快速上手 step by step
Step1:建立数据库 在使用Linq to Sql前,我们要将相应的数据库建好.在这个Demo中,使用的数据库是SQL Server Express 2005. 我们首先建立一个 ...
随机推荐
- [Java Web] 3、WEB开发之HTML基础程序试手
1.初试: <html> <body> <h1>My First Heading</h1> <p>My first paragraph.&l ...
- C语言实现二叉树
二叉树的重要性就不用多说啦: 我以前也学习过,但是一直没有总结: 网上找到的例子,要么是理论一大堆,然后是伪代码实现: 要么是复杂的代码,没有什么解释: 最终,还是靠FQ找到一些好的文章,参考地址我会 ...
- js运动:分享到
定时器及运动函数的使用. <!-- Author: XiaoWen Create a file: 2016-12-14 09:41:11 Last modified: 2016-12-14 10 ...
- JNI技术基础(2)——从零开始编写JNI代码
书接上文: <JNI技术基础(1)——从零开始编写JNI代码> 2.编译源程序HelloWorld.java并生成HelloWorld.class 3.生成头文件HelloWorld.h ...
- HTML常用命名和CSS reset代码【收集总结】
CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...
- MySQL Cluster 7.3.3 官方版本下载
MySQL Cluster 是 MySQL 适合于分布式计算环境的高实用.高冗余版本.它采用了NDB Cluster 存储引擎,允许在1个 Cluster 中运行多个MySQL服务器.在MyQL 5. ...
- NEWS - InstallShield 2014正式发布
InstallShield又迎来了新的版本InstallShield 2014,开发版本号Ver 21.0,相关产品信息已经可以从厂商Flexera Software(富莱睿)官方网站获得. 对于中国 ...
- html页面禁止自动填充浏览器记住的密码
现在的浏览器功能越来越强大,比如Chrome浏览器,在一个系统login的时候我们一般会记住密码,那么在整个系统中,浏览器一旦遇到 type="password"的控件,就会把密码 ...
- 在github上写博客
在github上混了几个月,收获颇多.作为一个开源的坚定信仰者,深深觉得每一个码农都应该参与到开源社区中,github提供了一个平台,让你为开源项目提交代码变得异常简单和直接.以前由于工作异常繁忙和繁 ...
- 基于Qt的遥感图像处理软件设计总结
开发工具 VS2008+Qt4.8.0+GDAL1.9 要点 接口要独立,软件平台与算法模块独立,平台中各接口设计灵活,修改时容易. 设计软件时一步步来,每个功能逐一实现,某个功能当比较独立时可以 ...