首页 / C# / 译:在C#中使用LINQ To SQL
译:在C#中使用LINQ To SQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了译:在C#中使用LINQ To SQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4276字,纯文字阅读大概需要7分钟。
内容图文
译文出处: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 class3privateint id; 4privatestring name; 5privatestring fname; 6privateint age; 7privatestring 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\Projects19 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 file22 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"); 7var selectQuery = from s in LTS.Students 8select s; 9foreach (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"); 11var delete = from p in LTS.Students 12where p.Id == iid 13select 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(); 8int iid = 0; 9 Console.WriteLine("Enter the Id of the student u want to update ?"); 10 iid = Convert.ToInt32(Console.ReadLine()); 1112 Console.WriteLine("Enter the new name of the student u want to update?"); 13string up = (Console.ReadLine()); 14var update = from s1 in LTS.Students 15where s1.Id == iid 16select s1; 17foreach (var v in update) 18 v.Name = up; 19 LTS.SubmitChanges(); 20 }
主函数:
1 static void Main(string[] arg){ 23 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.
原文:http://www.cnblogs.com/yplong/p/4274427.html
内容总结
以上是互联网集市为您收集整理的译:在C#中使用LINQ To SQL全部内容,希望文章能够帮你解决译:在C#中使用LINQ To SQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。