译文出处: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的更多相关文章

  1. VB.NET中使用Linq TO SQL添加数据后获得自增长列ID

    VB.NET中使用Linq TO SQL添加数据后获得自增长列ID: Dim tempOrdre As New Order With { .CustomerID = cmbCustomerName.S ...

  2. [转载代码]VB.NET 中查询 Linq to SQL 执行时的SQL语句

    在搜索使用LINQ TO SQL 添加数据后获得自增长ID的方法时,发现C#可以使用DebuggerWritter把使用Linq to SQL执行的SQL语句显示到即时窗口,于是在网上搜索到在VB.N ...

  3. wcf+linq to sql中关联查询返回数据问题

    前段时间准备采用wcf+nh框架开发sl程序,发现采用nh开发不适合我的中型.并且快速开发项目,所以综合考量了下,决定采用wcf+linq to sql . 但是此模式也有缺点,也是linq to s ...

  4. linq to sql 中增删改查

    首先我先说一下,如果真的要用linq做项目的话,也会是比较方便的.已经尝试了在三层架构中应用linq to sql 比较方便. //有三个不同的数据库表,所以写法不一样 public class Li ...

  5. 年终巨献 史上最全 ——LINQ to SQL语句

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  6. LINQ to SQL语句(18)之运算符转换

    运算符转换 1.AsEnumerable:将类型转换为泛型 IEnumerable 使用 AsEnumerable<TSource> 可返回类型化为泛型 IEnumerable 的参数.在 ...

  7. LINQ to SQL语句(13)之开放式并发控制和事务

    Simultaneous Changes开放式并发控制 下表介绍 LINQ to SQL 文档中涉及开放式并发的术语: 术语 说明 并发 两个或更多用户同时尝试更新同一数据库行的情形. 并发冲突 两个 ...

  8. LINQ TO SQL 大全

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 LINQ to SQL语句(1)之Where 适用场景: ...

  9. LINQ to SQL快速上手 step by step

    Step1:建立数据库      在使用Linq to Sql前,我们要将相应的数据库建好.在这个Demo中,使用的数据库是SQL Server Express 2005.      我们首先建立一个 ...

随机推荐

  1. ViEmuVS2013-3.2.1 破解

    VS升级到2013后,作为一个Vimer,自然需要更新最新的ViEmu插件,因为现在离了Vim,写代码已经寸步难行了. ViEmu 3.2.1的破解其实和Viemu 3.0.13的破解方法一样.安装前 ...

  2. JavaScript中for..in循环陷阱介绍

    for...in循环中的循环计数器是字符串,而不是数字它包含当前属性的名称或当前数组元素的索引,下面有个不错的示例大家可以参考下   大家都知道在JavaScript中提供了两种方式迭代对象: (1) ...

  3. crossplatform---Nodejs in Visual Studio Code 06.新建Module

    1.开始 Node.js:https://nodejs.org 2.Moudle js编程中,由于大家可以直接在全局作用域中编写代码,使开发人员可以很容易的新建一个全局变量或这全局模块,这些全局变量或 ...

  4. Maven学习总结(二)——Maven项目构建过程练习_转载

    上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项目的过程 一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立M ...

  5. C# winform的WebBrowser非常规编程(强烈推荐)

    本文章被今日头条推荐 1.在WebBrowser中实现抓取301和302协议 在WebBrowser中抓取301和302协议目前官方提供的组件远远不够,需要借助HttpMonitor.dll.这个组件 ...

  6. 水星Mercury路由器端口映射设置图文方法

    在一些内网的环境里,你可能需要把自己的内网的WEB服务器或者其他应用服务器设置成通过互联网可以访问,但是在内网我们是通过路由器共享上网的,外网无法访问到我们的内部服务器.那么这就需要我们通过" ...

  7. 解决POI读取Excel如何判断行是不是为空

    在作Excel表导入数据库的时候要统计成功导入了多少条,失败了多少条. 问题一:Excel表里有225行,只有3行是有数据的,但是我在读Excel表的时候它连没有数据的行也读进来了. 问题二:如果你是 ...

  8. eclipse web项目转maven项目

    ps:好久没写博客了,工作了人就懒了,加油加油,up,up 1 eclipse web项目目录 /web app src com.xx.xx *.properties *.xml WebRoot ​W ...

  9. 【OpenWRT】 Chaos Calmer 15.05 编译

    进入正题,编译环境准备完毕后,下载源码 git clone git://git.coding.net/leop/openwrt.git 复制代码 复制dl包(可以加快初次编译速度,但非必须)链接:pa ...

  10. Determining if a point lies on the interior of a polygon

    Determining if a point lies on the interior of a polygon Written by Paul Bourke  November 1987 Solut ...