譯文出處:http://www.codeproject.com/Tips/871938/LINQ-To-SQL-Using-Csharpweb
今天在這個話題中,我給你們分享一個在c#編程中很是有趣和十分有用的特性。sql
開始以前,我想告訴你們關於Linq的基本信息。好比:什麼是linq?而後再來分享實際應用。數據庫
說明:編程
LINQ = Language Integrated Query(集成查詢語言)c#
Linq是微軟在.NET Framework 3.5中信增長的一個特性。它是用來查詢數據庫和對數據庫查詢的本地集合帶來安全性。它很是簡單可是頗有組織性。一個普通的查詢語言,適用於SQL, XML, 本地collections 和 第三方APIs 好比SharePoint.安全
本質上,Linq提供的就是一個輕量級的編程數據集成。這是很是有價值的,尤爲是當今天天面對的數據和將來的大數據。服務器
接下來咱們就看看這個神祕的東東。函數
第一步:大數據
第二步:ui
我將聲明一些類,添加一些類成員。
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.