首先使用Vs.net 2003 Tools菜单中的connect to DataBase 命令连接到数据库(以本地数据库为例),然后你可以看到本地数据库中的相应的表结构,这里我建立一个users表。然后再建立一个Dataset实体,如图所示:

然后从Server Explorer 中拖拽users对象到Dataset视图中去,这时能看到视图中有一个users表的结构,然后点击保存。这时默认是自动生成Dataset的代理类。你可以把隐藏的文件都显示出来,然后看Dataset的cs文件,这个就是他的代理类。代码如下:

dataset代理类
1
//------------------------------------------------------------------------------
2
// <autogenerated>
3
// This code was generated by a tool.
4
// Runtime Version: 1.1.4322.2300
5
//
6
// Changes to this file may cause incorrect behavior and will be lost if
7
// the code is regenerated.
8
// </autogenerated>
9
//------------------------------------------------------------------------------
10
11
namespace TestXsd
{
12
using System;
13
using System.Data;
14
using System.Xml;
15
using System.Runtime.Serialization;
16
17
18
[Serializable()]
19
[System.ComponentModel.DesignerCategoryAttribute("code")]
20
[System.Diagnostics.DebuggerStepThrough()]
21
[System.ComponentModel.ToolboxItem(true)]
22
public class Dataset2 : DataSet
{
23
24
private usersDataTable tableusers;
25
26
public Dataset2()
{
27
this.InitClass();
28
System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
29
this.Tables.CollectionChanged += schemaChangedHandler;
30
this.Relations.CollectionChanged += schemaChangedHandler;
31
}
32
33
protected Dataset2(SerializationInfo info, StreamingContext context)
{
34
string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
35
if ((strSchema != null))
{
36
DataSet ds = new DataSet();
37
ds.ReadXmlSchema(new XmlTextReader(new System.IO.StringReader(strSchema)));
38
if ((ds.Tables["users"] != null))
{
39
this.Tables.Add(new usersDataTable(ds.Tables["users"]));
40
}
41
this.DataSetName = ds.DataSetName;
42
this.Prefix = ds.Prefix;
43
this.Namespace = ds.Namespace;
44
this.Locale = ds.Locale;
45
this.CaseSensitive = ds.CaseSensitive;
46
this.EnforceConstraints = ds.EnforceConstraints;
47
this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
48
this.InitVars();
49
}
50
else
{
51
this.InitClass();
52
}
53
this.GetSerializationData(info, context);
54
System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
55
this.Tables.CollectionChanged += schemaChangedHandler;
56
this.Relations.CollectionChanged += schemaChangedHandler;
57
}
58
59
[System.ComponentModel.Browsable(false)]
60
[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
61
public usersDataTable users
{
62
get
{
63
return this.tableusers;
64
}
65
}
66
67
public override DataSet Clone()
{
68
Dataset2 cln = ((Dataset2)(base.Clone()));
69
cln.InitVars();
70
return cln;
71
}
72
73
protected override bool ShouldSerializeTables()
{
74
return false;
75
}
76
77
protected override bool ShouldSerializeRelations()
{
78
return false;
79
}
80
81
protected override void ReadXmlSerializable(XmlReader reader)
{
82
this.Reset();
83
DataSet ds = new DataSet();
84
ds.ReadXml(reader);
85
if ((ds.Tables["users"] != null))
{
86
this.Tables.Add(new usersDataTable(ds.Tables["users"]));
87
}
88
this.DataSetName = ds.DataSetName;
89
this.Prefix = ds.Prefix;
90
this.Namespace = ds.Namespace;
91
this.Locale = ds.Locale;
92
this.CaseSensitive = ds.CaseSensitive;
93
this.EnforceConstraints = ds.EnforceConstraints;
94
this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
95
this.InitVars();
96
}
97
98
protected override System.Xml.Schema.XmlSchema GetSchemaSerializable()
{
99
System.IO.MemoryStream stream = new System.IO.MemoryStream();
100
this.WriteXmlSchema(new XmlTextWriter(stream, null));
101
stream.Position = 0;
102
return System.Xml.Schema.XmlSchema.Read(new XmlTextReader(stream), null);
103
}
104
105
internal void InitVars()
{
106
this.tableusers = ((usersDataTable)(this.Tables["users"]));
107
if ((this.tableusers != null))
{
108
this.tableusers.InitVars();
109
}
110
}
111
112
private void InitClass()
{
113
this.DataSetName = "Dataset2";
114
this.Prefix = "";
115
this.Namespace = "http://tempuri.org/Dataset2.xsd";
116
this.Locale = new System.Globalization.CultureInfo("en-US");
117
this.CaseSensitive = false;
118
this.EnforceConstraints = true;
119
this.tableusers = new usersDataTable();
120
this.Tables.Add(this.tableusers);
121
}
122
123
private bool ShouldSerializeusers()
{
124
return false;
125
}
126
127
private void SchemaChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
{
128
if ((e.Action == System.ComponentModel.CollectionChangeAction.Remove))
{
129
this.InitVars();
130
}
131
}
132
133
public delegate void usersRowChangeEventHandler(object sender, usersRowChangeEvent e);
134
135
[System.Diagnostics.DebuggerStepThrough()]
136
public class usersDataTable : DataTable, System.Collections.IEnumerable
{
137
138
private DataColumn columnUserID;
139
140
private DataColumn columnUsername;
141
142
private DataColumn columnPassword;
143
144
private DataColumn columnNickname;
145
146
private DataColumn columnEmail;
147
148
private DataColumn columnLastLoginTime;
149
150
private DataColumn columnGroupID;
151
152
private DataColumn columnPoint;
153
154
internal usersDataTable() :
155
base("users")
{
156
this.InitClass();
157
}
158
159
internal usersDataTable(DataTable table) :
160
base(table.TableName)
{
161
if ((table.CaseSensitive != table.DataSet.CaseSensitive))
{
162
this.CaseSensitive = table.CaseSensitive;
163
}
164
if ((table.Locale.ToString() != table.DataSet.Locale.ToString()))
{
165
this.Locale = table.Locale;
166
}
167
if ((table.Namespace != table.DataSet.Namespace))
{
168
this.Namespace = table.Namespace;
169
}
170
this.Prefix = table.Prefix;
171
this.MinimumCapacity = table.MinimumCapacity;
172
this.DisplayExpression = table.DisplayExpression;
173
}
174
175
[System.ComponentModel.Browsable(false)]
176
public int Count
{
177
get
{
178
return this.Rows.Count;
179
}
180
}
181
182
internal DataColumn UserIDColumn
{
183
get
{
184
return this.columnUserID;
185
}
186
}
187
188
internal DataColumn UsernameColumn
{
189
get
{
190
return this.columnUsername;
191
}
192
}
193
194
internal DataColumn PasswordColumn
{
195
get
{
196
return this.columnPassword;
197
}
198
}
199
200
internal DataColumn NicknameColumn
{
201
get
{
202
return this.columnNickname;
203
}
204
}
205
206
internal DataColumn EmailColumn
{
207
get
{
208
return this.columnEmail;
209
}
210
}
211
212
internal DataColumn LastLoginTimeColumn
{
213
get
{
214
return this.columnLastLoginTime;
215
}
216
}
217
218
internal DataColumn GroupIDColumn
{
219
get
{
220
return this.columnGroupID;
221
}
222
}
223
224
internal DataColumn PointColumn
{
225
get
{
226
return this.columnPoint;
227
}
228
}
229
230
public usersRow this[int index]
{
231
get
{
232
return ((usersRow)(this.Rows[index]));
233
}
234
}
235
236
public event usersRowChangeEventHandler usersRowChanged;
237
238
public event usersRowChangeEventHandler usersRowChanging;
239
240
public event usersRowChangeEventHandler usersRowDeleted;
241
242
public event usersRowChangeEventHandler usersRowDeleting;
243
244
public void AddusersRow(usersRow row)
{
245
this.Rows.Add(row);
246
}
247
248
public usersRow AddusersRow(string Username, string Password, string Nickname, string Email, System.DateTime LastLoginTime, short GroupID, int Point)
{
249
usersRow rowusersRow = ((usersRow)(this.NewRow()));
250
rowusersRow.ItemArray = new object[]
{
251
null,
252
Username,
253
Password,
254
Nickname,
255
Email,
256
LastLoginTime,
257
GroupID,
258
Point};
259
this.Rows.Add(rowusersRow);
260
return rowusersRow;
261
}
262
263
public usersRow FindByUserID(int UserID)
{
264
return ((usersRow)(this.Rows.Find(new object[]
{
265
UserID})));
266
}
267
268
public System.Collections.IEnumerator GetEnumerator()
{
269
return this.Rows.GetEnumerator();
270
}
271
272
public override DataTable Clone()
{
273
usersDataTable cln = ((usersDataTable)(base.Clone()));
274
cln.InitVars();
275
return cln;
276
}
277
278
protected override DataTable CreateInstance()
{
279
return new usersDataTable();
280
}
281
282
internal void InitVars()
{
283
this.columnUserID = this.Columns["UserID"];
284
this.columnUsername = this.Columns["Username"];
285
this.columnPassword = this.Columns["Password"];
286
this.columnNickname = this.Columns["Nickname"];
287
this.columnEmail = this.Columns["Email"];
288
this.columnLastLoginTime = this.Columns["LastLoginTime"];
289
this.columnGroupID = this.Columns["GroupID"];
290
this.columnPoint = this.Columns["Point"];
291
}
292
293
private void InitClass()
{
294
this.columnUserID = new DataColumn("UserID", typeof(int), null, System.Data.MappingType.Element);
295
this.Columns.Add(this.columnUserID);
296
this.columnUsername = new DataColumn("Username", typeof(string), null, System.Data.MappingType.Element);
297
this.Columns.Add(this.columnUsername);
298
this.columnPassword = new DataColumn("Password", typeof(string), null, System.Data.MappingType.Element);
299
this.Columns.Add(this.columnPassword);
300
this.columnNickname = new DataColumn("Nickname", typeof(string), null, System.Data.MappingType.Element);
301
this.Columns.Add(this.columnNickname);
302
this.columnEmail = new DataColumn("Email", typeof(string), null, System.Data.MappingType.Element);
303
this.Columns.Add(this.columnEmail);
304
this.columnLastLoginTime = new DataColumn("LastLoginTime", typeof(System.DateTime), null, System.Data.MappingType.Element);
305
this.Columns.Add(this.columnLastLoginTime);
306
this.columnGroupID = new DataColumn("GroupID", typeof(short), null, System.Data.MappingType.Element);
307
this.Columns.Add(this.columnGroupID);
308
this.columnPoint = new DataColumn("Point", typeof(int), null, System.Data.MappingType.Element);
309
this.Columns.Add(this.columnPoint);
310
this.Constraints.Add(new UniqueConstraint("Dataset2Key1", new DataColumn[]
{
311
this.columnUserID}, true));
312
this.columnUserID.AutoIncrement = true;
313
this.columnUserID.AllowDBNull = false;
314
this.columnUserID.ReadOnly = true;
315
this.columnUserID.Unique = true;
316
this.columnLastLoginTime.AllowDBNull = false;
317
this.columnGroupID.AllowDBNull = false;
318
this.columnPoint.AllowDBNull = false;
319
}
320
321
public usersRow NewusersRow()
{
322
return ((usersRow)(this.NewRow()));
323
}
324
325
protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
{
326
return new usersRow(builder);
327
}
328
329
protected override System.Type GetRowType()
{
330
return typeof(usersRow);
331
}
332
333
protected override void OnRowChanged(DataRowChangeEventArgs e)
{
334
base.OnRowChanged(e);
335
if ((this.usersRowChanged != null))
{
336
this.usersRowChanged(this, new usersRowChangeEvent(((usersRow)(e.Row)), e.Action));
337
}
338
}
339
340
protected override void OnRowChanging(DataRowChangeEventArgs e)
{
341
base.OnRowChanging(e);
342
if ((this.usersRowChanging != null))
{
343
this.usersRowChanging(this, new usersRowChangeEvent(((usersRow)(e.Row)), e.Action));
344
}
345
}
346
347
protected override void OnRowDeleted(DataRowChangeEventArgs e)
{
348
base.OnRowDeleted(e);
349
if ((this.usersRowDeleted != null))
{
350
this.usersRowDeleted(this, new usersRowChangeEvent(((usersRow)(e.Row)), e.Action));
351
}
352
}
353
354
protected override void OnRowDeleting(DataRowChangeEventArgs e)
{
355
base.OnRowDeleting(e);
356
if ((this.usersRowDeleting != null))
{
357
this.usersRowDeleting(this, new usersRowChangeEvent(((usersRow)(e.Row)), e.Action));
358
}
359
}
360
361
public void RemoveusersRow(usersRow row)
{
362
this.Rows.Remove(row);
363
}
364
}
365
366
[System.Diagnostics.DebuggerStepThrough()]
367
public class usersRow : DataRow
{
368
369
private usersDataTable tableusers;
370
371
internal usersRow(DataRowBuilder rb) :
372
base(rb)
{
373
this.tableusers = ((usersDataTable)(this.Table));
374
}
375
376
public int UserID
{
377
get
{
378
return ((int)(this[this.tableusers.UserIDColumn]));
379
}
380
set
{
381
this[this.tableusers.UserIDColumn] = value;
382
}
383
}
384
385
public string Username
{
386
get
{
387
try
{
388
return ((string)(this[this.tableusers.UsernameColumn]));
389
}
390
catch (InvalidCastException e)
{
391
throw new StrongTypingException("Cannot get value because it is DBNull.", e);
392
}
393
}
394
set
{
395
this[this.tableusers.UsernameColumn] = value;
396
}
397
}
398
399
public string Password
{
400
get
{
401
try
{
402
return ((string)(this[this.tableusers.PasswordColumn]));
403
}
404
catch (InvalidCastException e)
{
405
throw new StrongTypingException("Cannot get value because it is DBNull.", e);
406
}
407
}
408
set
{
409
this[this.tableusers.PasswordColumn] = value;
410
}
411
}
412
413
public string Nickname
{
414
get
{
415
try
{
416
return ((string)(this[this.tableusers.NicknameColumn]));
417
}
418
catch (InvalidCastException e)
{
419
throw new StrongTypingException("Cannot get value because it is DBNull.", e);
420
}
421
}
422
set
{
423
this[this.tableusers.NicknameColumn] = value;
424
}
425
}
426
427
public string Email
{
428
get
{
429
try
{
430
return ((string)(this[this.tableusers.EmailColumn]));
431
}
432
catch (InvalidCastException e)
{
433
throw new StrongTypingException("Cannot get value because it is DBNull.", e);
434
}
435
}
436
set
{
437
this[this.tableusers.EmailColumn] = value;
438
}
439
}
440
441
public System.DateTime LastLoginTime
{
442
get
{
443
return ((System.DateTime)(this[this.tableusers.LastLoginTimeColumn]));
444
}
445
set
{
446
this[this.tableusers.LastLoginTimeColumn] = value;
447
}
448
}
449
450
public short GroupID
{
451
get
{
452
return ((short)(this[this.tableusers.GroupIDColumn]));
453
}
454
set
{
455
this[this.tableusers.GroupIDColumn] = value;
456
}
457
}
458
459
public int Point
{
460
get
{
461
return ((int)(this[this.tableusers.PointColumn]));
462
}
463
set
{
464
this[this.tableusers.PointColumn] = value;
465
}
466
}
467
468
public bool IsUsernameNull()
{
469
return this.IsNull(this.tableusers.UsernameColumn);
470
}
471
472
public void SetUsernameNull()
{
473
this[this.tableusers.UsernameColumn] = System.Convert.DBNull;
474
}
475
476
public bool IsPasswordNull()
{
477
return this.IsNull(this.tableusers.PasswordColumn);
478
}
479
480
public void SetPasswordNull()
{
481
this[this.tableusers.PasswordColumn] = System.Convert.DBNull;
482
}
483
484
public bool IsNicknameNull()
{
485
return this.IsNull(this.tableusers.NicknameColumn);
486
}
487
488
public void SetNicknameNull()
{
489
this[this.tableusers.NicknameColumn] = System.Convert.DBNull;
490
}
491
492
public bool IsEmailNull()
{
493
return this.IsNull(this.tableusers.EmailColumn);
494
}
495
496
public void SetEmailNull()
{
497
this[this.tableusers.EmailColumn] = System.Convert.DBNull;
498
}
499
}
500
501
[System.Diagnostics.DebuggerStepThrough()]
502
public class usersRowChangeEvent : EventArgs
{
503
504
private usersRow eventRow;
505
506
private DataRowAction eventAction;
507
508
public usersRowChangeEvent(usersRow row, DataRowAction action)
{
509
this.eventRow = row;
510
this.eventAction = action;
511
}
512
513
public usersRow Row
{
514
get
{
515
return this.eventRow;
516
}
517
}
518
519
public DataRowAction Action
{
520
get
{
521
return this.eventAction;
522
}
523
}
524
}
525
}
526
}
527
你可以用代理类提供的方法方便的对xml文件进行操作,代码如下:
1
Dataset2 ds = new Dataset2() ;
2
ds.ReadXml("test.xml") ;
3
4
Dataset2.usersRow row = ds.users.NewusersRow() ;
5
row.Username="test";
6
row.Email ="test@test.com";
7
row.Password ="test";
8
row.Nickname ="test";
9
row.LastLoginTime =DateTime.Now ;
10
row.GroupID =1;
11
row.Point =100;
12
ds.users.AddusersRow(row) ;
13
14
ds.WriteXml("test.xml") ;
这种方法非常方便,使用拖拽的方式生成xml schemal ,当然事先你也可以自己定义好schemal后 ,再生成代理类,偶是个懒人觉得这种方式比较适合我!如果你有什么好的想法都可以回复到下面!