译文出处: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 文件扩展上.
第二步:
我将声明一些类,添加一些类成员。
1 class Program
2 { // this is program class
3 private int id;
4 private string name;
5 private string fname;
6 private int age;
7 private string sem;
8 }
第三步:
这里我将告诉大家如何通过 LINQ-To-SQL 向数据库中插入数据。
1 public void insert()
2 {
3 // these are the class data members through we will send our objects data in the database
4 Console.WriteLine("Enter id");
5 id = Convert.ToInt32(Console.ReadLine());
6 Console.WriteLine("Enter name");
7 name = Console.ReadLine();
8 Console.WriteLine("Enter father name");
9 fname = Console.ReadLine();
10 Console.WriteLine("Enter age");
11 age = Convert.ToInt32(Console.ReadLine());
12 Console.WriteLine("Enter semester");
13 sem = Console.ReadLine();
14 // this is the data context class the main class which handles
15 // all the functionality in this will pass the connection string of our database file.
16 LTSDataContext LTS = new LTSDataContext
17 (@"Data Source=(LocalDB)\v11.0;
18 AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
19 ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
20 Integrated Security=True;Connect Timeout=30");
21 // this is the table class which we drag on our linq to sql file
22 Student objStudentTable = new Student();
23 objStudentTable.Id = id;
24 objStudentTable.Name = name;
25 objStudentTable.Father_Name = fname;
26 objStudentTable.Age = age;
27 objStudentTable.Semester = sem;
28 LTS.Students.InsertOnSubmit(objStudentTable); // this is built in function.
29 LTS.SubmitChanges();// here is the final query will run in the data context class.
30 }
第四步:展示数据
1 void Display()
2 {
3 LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
4 AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
5 ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
6 Integrated Security=True;Connect Timeout=30");
7 var selectQuery = from s in LTS.Students
8 select s;
9 foreach (Student s in selectQuery)
10 {
11 Console.WriteLine(s.Id + "\t" + s.Name + "\t" +
12 s.Father_Name + "\t" + s.Age + "\t" + s.Semester);
13 }
14 }
第五步:删除数据
1 void Delete()
2 {
3 int iid = 0;
4 Console.WriteLine("Enter the Id of the student u want to delete?");
5 iid = Convert.ToInt32(Console.ReadLine());
6
7 LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
8 AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
9 ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
10 Integrated Security=True;Connect Timeout=30");
11 var delete = from p in LTS.Students
12 where p.Id == iid
13 select p;
14 LTS.Students.DeleteAllOnSubmit(delete);
15 LTS.SubmitChanges();
16 Student objStudentTable = LTS.Students.Single(c=> c.Id == iid);
17 }
第六步:更新数据
1 void update()
2 {
3 LTSDataContext LTS = new LTSDataContext(@"Data Source=(LocalDB)\v11.0;
4 AttachDbFilename=C:\Users\Ehtesham Mehmood\Documents\Visual Studio 2012\Projects\
5 ConsoleApplication5\ConsoleApplication5\SDatabase.mdf;
6 Integrated Security=True;Connect Timeout=30");
7 Student objStudentTable = new Student();
8 int iid = 0;
9 Console.WriteLine("Enter the Id of the student u want to update ?");
10 iid = Convert.ToInt32(Console.ReadLine());
11
12 Console.WriteLine("Enter the new name of the student u want to update?");
13 string up = (Console.ReadLine());
14 var update = from s1 in LTS.Students
15 where s1.Id == iid
16 select s1;
17 foreach (var v in update)
18 v.Name = up;
19 LTS.SubmitChanges();
20 }
主函数:
1 static void Main(string[] arg){
2
3 Program p1 = new Program(); // creates object
4 p1.insert();
5 p1.Display();
6 p1.Delete();
7 p1.update();
8 Console.ReadKey();
9 }
感谢您的阅读,请留下您的足迹。
Cheers! Enjoy coding.