描述一個資料表
1
/// <summary>
2
/// 描述一個資料表
3
/// </summary>
4
public class Table
5
{
6
7
protected Table(string name, ColumnCollection cols)
8
9
TableName = name;
10
Columns = cols;
11
Rows = new RowCollection();
12
}
13
14
15
/// 擷取或設定表的列架構
16
17
protected readonly ColumnCollection Columns;
18
19
/// 擷取或設定表的資料行
20
21
protected RowCollection Rows;
22
23
/// 擷取或設定表的名稱
24
25
protected readonly string TableName;
26
27
28
/// 将該行資料狀态改為RowState.Unchanged
29
30
protected void AcceptChanges()
31
32
for (int i = 0; i <= Rows.Count - 1; i++)
33
34
Rows[i].AcceptChanges();
35
36
37
38
39
/// 移除所有資料行
40
41
protected void Clear()
42
43
this.Rows.Clear();
44
45
46
47
/// 傳回狀态比對的資料行,并以新資料表的形式展現
48
49
/// <param name="rowState"></param>
50
/// <returns></returns>
51
protected Table GetChanges(RowState rowState)
52
53
Table tmpTable = new Table(rowState.ToString(), this.Columns);
54
foreach (Row row in Rows)
55
56
if (row.RowState == rowState)
57
58
tmpTable.Rows.Add(row);
59
60
61
return tmpTable;
62
63
64
65
66
/// 依據現有的資料表的架構,生成新的資料行
67
68
69
protected Row NewRow()
70
71
return new Row(Columns, this.TableName);
72
73
74
75
/// 按關鍵字(資料行的第一列)比對,并傳回符合的資料行
76
77
/// <param name="filterKey"></param>
78
79
protected Row[] Select(string filterKey)
80
81
System.Collections.ArrayList tmpRows = new System.Collections.ArrayList();
82
83
84
if (row[0].ToString() == filterKey)
85
86
tmpRows.Add(row);
87
88
89
return (Row[])tmpRows.ToArray(typeof(Row));
90
91
92
93
/// 列印Table的資料
94
95
public virtual void Print()
96
97
System.Text.StringBuilder sb = new StringBuilder();
98
99
foreach (Column col in this.Columns)
100
101
sb.AppendFormat("{0}/t", col.ColumnName);
102
103
sb.AppendLine();
104
System.Console.WriteLine(sb.ToString());
105
foreach (Row row in this.Rows)
106
107
foreach (object obj in row.ItemArray)
108
109
System.Console.Write("{0}/t", obj);
110
111
System.Console.WriteLine();
112
113
114
好,到此Table所涉及的5個類都定義實作完畢了。
我們來回顧一下,Table中什麼是最基礎的,什麼是最重要的?
按我們一般來看,表中最重要基礎的實行,最重要的是資料。其實表最基礎是的架構,架構就是列的定義,而行隻不過是列的資料展現形式。
是以Table在構造的時候,必須先定義列的集合,而Row的構造函數是protected internal的,意義是不能在外面被構造,必須通過Table來依據列的定義來構造。
下篇,我們将看到我們對Table的繼承,發揮更多OO展現。
本文轉自shyleoking 51CTO部落格,原文連結:http://blog.51cto.com/shyleoking/806271