Step-By-Step教學:改寫LINQ to SQL資料異動行為

摘要:Step-By-Step教學:改寫LINQ to SQL資料異動行為

原文:http://vmiv.blogspot.com/2008/03/linq-to-sql.html

LINQ to SQL提供一個簡單的資料存取模型,讓程式設計師能夠很容易地新增資料到資料庫,刪除資料庫資料,或對資料庫中既有的資料進行修改。預設LINQ to SQL會自動產生異動資料的T-SQL敘述來進行更新作業,但在預設行為不敷所需時,也可以很彈性地進行客製化動作。以下步驟說明客製化方式。

1. 建立一個Windows表單專案。
2. 加入LINQ to SQL類別,命為Northwind.dbml。

3. 開啟Server Explorer工具,連接到Northwind資料庫,然後將Region資料表拖曳到LINQ to SQL設計畫面左方的區塊之中。

4. 在伺服器總管,Northwind.dbml檔案方案滑鼠右鍵,選取「View Code」,Visual Studio 2008將會建立一個Northwind.cs檔案,其中包含一個NorthwindDataContext部分類別的定義。

5. 在NorthwindDataContext類別的大括號中,輸入partial一個字,Visual Studio會自動列出所有可以改寫的部分方法,包含InsertRegion、OnCreated、DeleteRegion與UpdateRegion方法,選取任一方法後,按Tab鍵,Visual Studio會自動產生方法的函式定義。

6. 修改Northwind.cs

01 using System.Data;
02
03 using System.Data.SqlClient;

04
05 namespace CustomLTSQL {
06
07     partial class NorthwindDataContext
08     {
09
10         partial void InsertRegion(Region instance)
11         {
12
13             SqlConnection cn = new SqlConnection(this.Connection.ConnectionString);
14
15             SqlCommand cmd = new SqlCommand("Insert into Region values(@RegionID,@RegionDescription)", cn);
16
17             cmd.Parameters.AddWithValue("@RegionID", instance.RegionID);
18
19             cmd.Parameters.AddWithValue("@RegionDescription", instance.RegionDescription);
20
21             cn.Open();
22
23             cmd.ExecuteNonQuery();
24
25             cn.Close();
26
27         }

28
29         partial void DeleteRegion(Region instance)
30         {
31
32             SqlConnection cn = new SqlConnection(this.Connection.ConnectionString);
33
34             SqlCommand cmd = new SqlCommand("delete from Region where RegionID=@RegionID", cn);
35
36             cmd.Parameters.AddWithValue("@RegionID", instance.RegionID);
37
38             cn.Open();
39
40             cmd.ExecuteNonQuery();
41
42             cn.Close();
43
44         }

45
46         partial void UpdateRegion(Region instance)
47         {
48
49             SqlConnection cn = new SqlConnection(this.Connection.ConnectionString);
50
51             SqlCommand cmd = new SqlCommand("update Region set RegionDescription=@RegionDescription where RegionID=@RegionID", cn);
52
53             cmd.Parameters.AddWithValue("@RegionID", instance.RegionID);
54
55             cmd.Parameters.AddWithValue("@RegionDescription", instance.RegionDescription);
56
57             cn.Open();
58
59             cmd.ExecuteNonQuery();
60
61             cn.Close();
62
63         }

64
65     }

66
67 }

68
69

7. 在表單中加入三個Button,分別為Insert、Update、Delete,然後在其Click事件處理常式加入以下程式,分別進行新增、刪除、修改資料的動作:

01 using System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Linq;
07 using System.Text;
08 using System.Windows.Forms;

09
10 namespace CustomLTSQL
11 {
12
13     public partial class Form1 : Form
14     {
15
16         public Form1()
17         {
18
19             InitializeComponent();
20
21         }

22
23         private void button1_Click(object sender, EventArgs e)
24         {
25
26             NorthwindDataContext db = new NorthwindDataContext();
27
28             Region r = new Region();
29
30             r.RegionID = 999;
31
32             r.RegionDescription = "new Region";
33
34             db.Regions.InsertOnSubmit(r);
35
36             db.SubmitChanges();
37
38         }

39
40         private void button2_Click(object sender, EventArgs e)
41         {
42
43             NorthwindDataContext db = new NorthwindDataContext();
44
45             Region reg = (from r in db.Regions
46
47                           where r.RegionID == 999
48
49                           select r).Single<Region>();
50
51             db.Regions.DeleteOnSubmit(reg);
52
53             db.SubmitChanges();
54
55         }

56
57         private void button3_Click(object sender, EventArgs e)
58         {
59
60             NorthwindDataContext db = new NorthwindDataContext();
61
62             Region reg = (from r in db.Regions
63
64                           where r.RegionID == 999
65
66                           select r).Single<Region>();
67
68             reg.RegionDescription = "Super new Region";
69
70             db.SubmitChanges();
71
72         }

73
74     }

75
76 }

77
78
79
80