1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
using IBatisNet.DataMapper;
13
using System.Reflection;
14
15
/// <summary>
16
/// ODRM为结合ORM与DataSet,并自动根据O和DataSet生成对象,以便业务层处理
17
/// </summary>
18
public partial class ODRM_test : PageBase
19
{
20
protected void Page_Load(object sender, EventArgs e)
21
{
22
if (!IsPostBack)
23
{
24
DataSet set11 = Mapper.Instance().QueryForDataSet("SelectXTM_UserByKey_Test",UIhashtable);
25
DataTable table1 = ConvertDataTable(set11, "");
26
//这里为自己定义的序列化类
27
cXTM_User[] objModel = new cXTM_User[table1.Rows.Count];
28
//DataTable转化为序列化类数组
29
for (int y = 0; y < table1.Rows.Count; y++)
30
{
31
objModel[y] = new cXTM_User();
32
DataTableConvertObject(table1.Rows[y], objModel[y]);
33
}
34
//以DataSet模式绑定
35
ExDataGrid1.DataSource = table1;
36
//以序列化对象模式绑定
37
//ExDataGrid1.DataSource = objModel;
38
ExDataGrid1.DataBind();
39
}
40
}
41
42
protected void ExDataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
43
{
44
/*
45
* 该部分应用范围
46
* 查询一条数据的修改,可以用objModel.UserName
47
* 而不必再使用DataTable[0].Rows[0]["UserName"]的模式
48
* 提高面向对象的程度,并减少业务流程部分编码
49
*/
50
51
if (e.Item.ItemIndex != –1)
52
{
53
cXTM_User objModel = new cXTM_User();
54
55
//如果为DataSet填充的DataGrid
56
if (e.Item.DataItem.GetType().FullName == "System.Data.DataRowView")
57
{
58
DataTableConvertObject((DataRow)((DataRowView)e.Item.DataItem).Row, objModel);
59
}
60
//否则认为为序列化对象填充
61
else
62
{
63
objModel = (cXTM_User)e.Item.DataItem;
64
65
}
66
}
67
}
68
69
#region 指定对象函数
70
/// <summary>
71
/// 数据集中一行DataRow转换为指定对象,并填充数据
72
/// </summary>
73
/// <param name="row">数据集中一行</param>
74
/// <param name="objModel">指定对象</param>
75
private void DataTableConvertObject(DataRow row, cXTM_User objModel)
76
{
77
Hashtable hTable = new Hashtable();
78
hTable = DataRowConvertHashtable(row);
79
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
80
81
for (int j = 0; j < objModel.Propertylist.Length; j++)
82
{
83
PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
84
propertyinfo.SetValue(objModel, hTable[objModel.Propertylist[j]], null);
85
}
86
}
87
88
/// <summary>
89
/// 对象转换为哈希表
90
/// </summary>
91
/// <param name="objModel">有数据的对象</param>
92
/// <returns>填充数据后的哈希表</returns>
93
public Hashtable ObjectConvertHashtable(cXTM_User objModel)
94
{
95
Hashtable hTable = new Hashtable();
96
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
97
for (int j = 0; j < objModel.Propertylist.Length; j++)
98
{
99
PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
100
hTable.Add(objModel.Propertylist[j], propertyinfo.GetValue(objModel, null));
101
}
102
return hTable;
103
}
104
105
/// <summary>
106
/// 对象转换为DataTable,并有单行DataRow
107
/// </summary>
108
/// <param name="objModel">有数据的对象</param>
109
/// <returns></returns>
110
public DataTable ObjectConvertDataTableWidthRow(cXTM_User objModel)
111
{
112
return ObjectConvertDataTableWidthRow(objModel, "");
113
}
114
115
/// <summary>
116
/// 对象转换为DataTable,并有单行DataRow
117
/// </summary>
118
/// <param name="objModel">有数据的对象</param>
119
/// <returns></returns>
120
public DataTable ObjectConvertDataTableWidthRow(cXTM_User objModel, string DataMapper)
121
{
122
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
123
DataTable dt = new DataTable();
124
if (DataMapper != "")
125
{
126
dt = new DataTable(DataMapper);
127
}
128
dt.Columns.Clear();
129
for (int j = 0; j < objModel.Propertylist.Length; j++)
130
{
131
PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
132
dt.Columns.Add(new DataColumn(objModel.Propertylist[j], propertyinfo.GetType()));
133
}
134
DataRow row = dt.NewRow();
135
for (int j = 0; j < objModel.Propertylist.Length; j++)
136
{
137
PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
138
row[objModel.Propertylist[j]] = propertyinfo.GetValue(objModel, null);
139
}
140
dt.Rows.Add(row);
141
142
return dt;
143
}
144
145
/// <summary>
146
/// 对象转换为DataTable,并有多行DataRow
147
/// </summary>
148
/// <param name="objModel">有数据的对象</param>
149
/// <returns></returns>
150
public DataTable ObjectConvertDataTableWidthRows(cXTM_User[] objModel)
151
{
152
return ObjectConvertDataTableWidthRows(objModel, "");
153
}
154
155
/// <summary>
156
/// 对象转换为DataTable,并有多行DataRow
157
/// </summary>
158
/// <param name="objModel">有数据的对象</param>
159
/// <returns></returns>
160
public DataTable ObjectConvertDataTableWidthRows(cXTM_User[] objModel, string DataMapper)
161
{
162
Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
163
DataTable dt = new DataTable();
164
if (DataMapper != "")
165
{
166
dt = new DataTable(DataMapper);
167
}
168
if (objModel.Length == 0)
169
{
170
return dt;
171
}
172
dt.Columns.Clear();
173
for (int j = 0; j < objModel[0].Propertylist.Length; j++)
174
{
175
PropertyInfo propertyinfo = entitytype.GetProperty(objModel[0].Propertylist[j]);
176
dt.Columns.Add(new DataColumn(objModel[0].Propertylist[j], propertyinfo.GetType()));
177
}
178
179
for (int i = 0; i < objModel.Length; i++)
180
{
181
DataRow row = dt.NewRow();
182
for (int j = 0; j < objModel[i].Propertylist.Length; j++)
183
{
184
PropertyInfo propertyinfo = entitytype.GetProperty(objModel[i].Propertylist[j]);
185
row[objModel[i].Propertylist[j]] = propertyinfo.GetValue(objModel[i], null);
186
}
187
dt.Rows.Add(row);
188
}
189
return dt;
190
}
191
#endregion
192
193
#region 通用函数
194
195
/// <summary>
196
/// 转换为DataTable
197
/// </summary>
198
/// <param name="Source">数据源</param>
199
/// <param name="DataMember">数据表名称</param>
200
public static DataTable ConvertDataTable(object Source, string DataMember)
201
{
202
DataTable baseTable = new DataTable();
203
if (Source is DataTable)
204
{
205
baseTable = (DataTable)Source;
206
return baseTable;
207
}
208
if (Source is DataSet)
209
{
210
211
DataSet set1 = (DataSet)Source;
212
if ((set1.Tables.Count > 1) && ((DataMember == null) || (DataMember == "")))
213
{
214
throw new Exception("If there is more than one table in your dataset, you must define the DataMember property to specify which table to use.");
215
}
216
if (set1.Tables.Count < 1)
217
{
218
throw new Exception("There are no tables in the datasource.");
219
}
220
if ((DataMember != null) && (DataMember != ""))
221
{
222
baseTable = set1.Tables[DataMember];
223
return baseTable;
224
}
225
else
226
{
227
baseTable = set1.Tables[0];
228
return baseTable;
229
}
230
231
}
232
return baseTable;
233
}
234
235
/// <summary>
236
/// 返回DataTable为哈希表键值对
237
/// </summary>
238
/// <param name="SourceTable">数据行对象</param>
239
/// <returns>填充后哈希表</returns>
240
public static Hashtable DataRowConvertHashtable(DataRow SourceRow)
241
{
242
Hashtable hTable = new Hashtable();
243
IList list = SourceRow.ItemArray;
244
object[] tObj = new object[SourceRow.Table.Columns.Count];
245
246
for (int i = 0; i < SourceRow.Table.Columns.Count; i++)
247
{
248
tObj[SourceRow.Table.Columns.IndexOf(SourceRow.Table.Columns[i].ColumnName)] = SourceRow.Table.Columns[i].ColumnName;
249
}
250
251
for (int x = 0; x < list.Count; x++)
252
{
253
hTable.Add(tObj[x].ToString(), list[x]);
254
}
255
return hTable;
256
}
257
258
#endregion
259
260
261
262
263
}
264

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264
