譯:在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 文件擴展上.

第二步:

我將聲明一些類,添加一些類成員。

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. Smile | :)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM