VS Studio可以帮助我们通过简单的拖拽生成Typed DataSet。同时自动生成了DataAdapter,更加简化了数据的访问操作。
下面是我基于VS 2005编写的一个类,为基于Typed DataSet的数据访问提供了比较简单的方法。
基类使用泛型,反射等方法实现了数据访问的封装。在定义实体类时,只需要继承该基类就可以实现数据的读取、保存和删除操作。
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Reflection;
5
using System.Data;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace SimpleCode.Core.Model
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public class ModelBase<TAdapter, TDataTable, TRow> : SimpleCode.Core.Interface.IModel
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Variable define#region Variable define
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
private object m_Adapter;
15
private Type m_TypeAdpt;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
private object m_Table;
19
private Type m_TypeTable;
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
private object m_Row;
23
private Type m_TypeRow;
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
private TRow m_CurrentItem;
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
private TDataTable m_Items;
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
private string m_ItemId;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
private string m_TableName;
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
#endregion
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Property define#region Property define
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
40
/// 适配器
41
/// </summary>
42
public TAdapter Apapter
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (TAdapter)m_Adapter; }
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
48
/// 数据表
49
/// </summary>
50
public TDataTable Table
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return (TDataTable)m_Table; }
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
56
/// 数据表中的所有数据
57
/// </summary>
58
public TDataTable Items
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
get
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
if (m_Items == null)
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
this.m_Items = GetItems();
65
}
66
return m_Items;
67
}
68
}
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
71
/// 当前的数据项
72
/// </summary>
73
public TRow CurrentItem
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ if (this.m_CurrentItem != null) return this.m_CurrentItem; else return this.m_CurrentItem = NewRow(); }
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ this.m_CurrentItem = value; }
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
#endregion
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructor#region Constructor
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
public ModelBase()
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
Initialize();
86
}
87![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
public ModelBase(string id)
89
: this()
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
this.m_ItemId = id;
92
m_CurrentItem = GetItemById(this.m_ItemId);
93
}
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
96
/// 初始化
97
/// </summary>
98
private void Initialize()
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
//初始化适配器
101
m_TypeAdpt = typeof(TAdapter);
102
m_Adapter = Activator.CreateInstance<TAdapter>();
103![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
//初始化数据表
105
m_TypeTable = typeof(TDataTable);
106
m_Table = Activator.CreateInstance<TDataTable>();
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
//初始化数据行
109
m_TypeRow = typeof(TRow);
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
this.m_TableName = ((DataTable)this.m_Table).TableName;
112
}
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
#endregion
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Method#region Private Method
117![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
119
/// 获取所有数据
120
/// </summary>
121
/// <returns></returns>
122
private TDataTable GetItems()
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
MethodInfo method = this.m_TypeAdpt.GetMethod("GetData");
125
return (TDataTable)method.Invoke(this.m_Adapter, null);
126
}
127![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
128![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
129
/// 删除数据
130
/// </summary>
131
/// <param name="rows"></param>
132
private void DeleteRow(TRow[] rows)
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
foreach (TRow row in rows)
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
DataRow dr = row as DataRow;
137
dr.Delete();
138
}
139![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update", new Type[]
{ typeof(System.Data.DataRow[]) });
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] parameters =
{ rows };
141
methodUpdate.Invoke(this.m_Adapter, parameters);
142
}
143![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
145
/// 删除当前数据
146
/// </summary>
147
private void DeleteRow()
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
149
DataRow dr = this.m_CurrentItem as DataRow;
150
dr.Delete();
151![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update", new Type[]
{ typeof(System.Data.DataRow) });
153![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
object[] parameters =
{ this.m_CurrentItem };
154
methodUpdate.Invoke(this.m_Adapter, parameters);
155
}
156![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
158
/// 新建数据项
159
/// </summary>
160
/// <returns></returns>
161
private TRow NewRow()
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
MethodInfo method = this.m_TypeTable.GetMethod(string.Format("New{0}Row", this.m_TableName));
164
return (TRow)method.Invoke(this.m_Table, null);
165
}
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
168
/// 保存当前数据
169
/// </summary>
170
/// <returns></returns>
171
private void SaveItem()
172![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
173
DataRow dr = this.m_CurrentItem as DataRow;
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175
//如果DataRowState为Detached,则为新增数据
176
if (dr.RowState == DataRowState.Detached)
177![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
178
((DataTable)this.m_Table).Rows.Add(dr);
179![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update", new Type[]
{ typeof(TDataTable) });
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
methodUpdate.Invoke(this.m_Adapter, new object[]
{ (TDataTable)this.m_Table });
182
}
183
else
184![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update", new Type[]
{ typeof(DataRow) });
186![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
methodUpdate.Invoke(this.m_Adapter, new object[]
{ this.m_CurrentItem });
187
}
188
}
189![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
190
#endregion
191![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
192![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Method#region Public Method
193![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
194![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
195
/// 获取复合条件的数据
196
/// </summary>
197
/// <param name="filter">筛选条件</param>
198
/// <returns></returns>
199
public TRow[] GetItems(string filter)
200![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
201
DataTable dt = this.Items as DataTable;
202
TRow[] rows = dt.Select(filter) as TRow[];
203![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
204
if (rows.Length > 0)
205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
206
return rows;
207
}
208
return default(TRow[]);
209
}
210![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
212
/// 获取制定Id的数据行
213
/// </summary>
214
/// <param name="id"></param>
215
/// <returns></returns>
216
public TRow GetItemById(string id)
217![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
218![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
219
DataTable dt = this.Items as DataTable;
220
//获取表的主键
221
DataColumn[] cols = dt.PrimaryKey;
222![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
223
//ToDo:考虑多主键的情况
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225
string filter = string.Format("{0} = '{1}'", cols[0].ColumnName, id);
226![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
227
TRow[] rows = dt.Select(filter) as TRow[];
228
if (rows.Length > 0)
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
return rows[0];
231
}
232
return default(TRow);
233
}
234![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
235![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IModel Members#region IModel Members
236![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
237
public bool Delete()
238![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
239
try
240![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
241
//如果数据有主外键关系,则删除不成功。
242
DeleteRow();
243
return true;
244
}
245
catch (Exception ex)
246![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
247
throw new Exception(ex.StackTrace);
248
}
249
}
250![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
251
public bool Save()
252![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
253
try
254![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
255
SaveItem();
256
return true;
257
}
258
catch (Exception ex)
259![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
260
throw new Exception(ex.StackTrace);
261
}
262
}
263![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
264
#endregion
265![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
266
#endregion
267![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
268
}
269
}
270![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在数据层中创建Typed DataSet
![](https://www.cnblogs.com/images/cnblogs_com/wenjielee/DataModel.jpg)
在业务层中定义Customer类
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using SimpleCode.Demo.DataModel;
5
using SimpleCode.Demo.DataModel.DemoDataTableAdapters;
6
using SimpleCode.Core.Model;
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
namespace SimpleCode.Demo.BL
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10
public class Customer : ModelBase<CustomerTableAdapter,DemoData.CustomerDataTable,DemoData.CustomerRow>
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Customer()
{ }
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Customer(string id):base(id)
{}
15
}
16
}
在应用层中可以通过以下方式访问
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using SimpleCode.Demo.BL;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
namespace SimpleCode.Demo.Test
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8
class Program
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
static void Main(string[] args)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
Customer myCustomer = new Customer();
13
myCustomer.CurrentItem.CustomerID = "00125";
14
myCustomer.CurrentItem.Name = "Jason Lee";
15
myCustomer.CurrentItem.Address = "Shanghai";
16
//Add Customer Informations
17
myCustomer.Save();
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
Customer myCustomer2 = new Customer("00125");
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
Console.WriteLine("Customer Name is " + myCustomer2.CurrentItem.Name);
22
}
23
}
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)