四.插入数据记录:
1<p> 对数据库进行插入记录操作和删除记录操作基本的思路是一致的,就是通过 ADO.NET 首先插入数据记录到数据库,然后对 "DataSet" 对象进行必要的修改。下列代码就是以 Access 2000 数据库为模型修改当前记录的代码: </p>
1<p>protected void Update_record ( object sender , System.EventArgs e )
2
3{
4
5int i = myBind.Position ;
6
7try{
8
9file:// 连接到一个数据库
10
11string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;
12
13OleDbConnection myConn = new OleDbConnection ( strCon ) ;
14
15myConn.Open ( ) ;
16
17myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;
18
19file:// 从数据库中修改指定记录
20
21string strUpdt = " UPDATE person SET xm = '"
22
23\+ t_xm.Text + "' , xb = '"
24
25\+ t_xb.Text + "' , nl = "
26
27\+ t_nl.Text + " , zip = "
28
29\+ t_books.Text + " WHERE id = " + t_id.Text ;
30
31OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;
32
33myCommand.ExecuteNonQuery ( ) ;
34
35myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;
36
37myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
38
39myConn.Close ( ) ;
40
41}
42
43catch ( Exception ed )
44
45{
46
47MessageBox.Show ( " 修改指定记录错误: " + ed.ToString ( ) , " 错误! " ) ;
48
49}
50
51myBind.Position = i ;
52
53} </p>
1<p> 由于对 Sql Server 2000 数据记录修改操作和 Access 2000 数据记录修改操作的差异只在于不同的数据链接,具体的代码可以参考 " 删除数据记录 " 中的代码,在这里就不提供了。
2
3五.插入数据记录:
4
5和前面二种操作在思路是一致的,就是通过 ADO.NET 首先插入数据记录到数据库,然后对 "DataSet" 对象进行必要的修改。下列代码就是以 Access 2000 数据库为模型插入一条数据记录的代码
6
7protected void Insert_record ( object sender , System.EventArgs e )
8
9{
10
11try
12
13{
14
15file:// 判断所有字段是否添完,添完则执行,反之弹出提示
16
17if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" )
18
19{
20
21string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
22
23OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
24
25myConn.Open ( ) ;
26
27string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;
28
29strInsert += t_id.Text + ", '" ;
30
31strInsert += t_xm.Text + "', '" ;
32
33strInsert += t_xb.Text + "', " ;
34
35strInsert += t_nl.Text + ", " ;
36
37strInsert += t_books.Text + ")" ;
38
39OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
40
41inst.ExecuteNonQuery ( ) ;
42
43myConn.Close ( ) ;
44
45myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;
46
47myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;
48
49myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
50
51}
52
53else
54
55{
56
57MessageBox.Show ( " 必须填满所有字段值! " , " 错误! " ) ;
58
59}
60
61}
62
63catch ( Exception ed )
64
65{
66
67MessageBox.Show ( " 保存数据记录发生 " + ed.ToString ( ) , " 错误! " ) ;
68
69}
70
71}
72
73同样对 Sql Server 2000 数据库进行插入记录操作和 Access 2000 数据库插入记录操作的差异也只在于不同的数据链接,具体的代码可以参考 " 删除数据记录 " 中的代码,在这里就不提供了。
74
75六. Visual C #数据库编程的完成源代码和程序运行的主界面:
76
77掌握了上面要点,编写一个完整的数据库编程的程序就显得非常容易了,下面是 Visual C #进行数据库编程的完整代码( Data01.cs ),此代码是以 Access 2000 数据库为模型设计的,具体如下:
78
79using System ;
80
81using System.Drawing ;
82
83using System.ComponentModel ;
84
85using System.Windows.Forms ;
86
87using System.Data.OleDb ;
88
89using System.Data ;
90
91public class Data : Form
92
93{
94
95private System.ComponentModel.Container components = null ;
96
97private Button lastrec ;
98
99private Button nextrec ;
100
101private Button previousrec ;
102
103private Button firstrec ;
104
105private TextBox t_books ;
106
107private TextBox t_nl ;
108
109private ComboBox t_xb ;
110
111private TextBox t_xm ;
112
113private TextBox t_id ;
114
115private Label l_books ;
116
117private Label l_nl ;
118
119private Label l_xb ;
120
121private Label l_xm ;
122
123private Label l_id ;
124
125private Label label1 ;
126
127private DataSet myDataSet ;
128
129private Button button1 ;
130
131private Button button2 ;
132
133private Button button3 ;
134
135private Button button4 ;
136
137private BindingManagerBase myBind ;
138
139public Data ( )
140
141{
142
143file:// 连接到一个数据库
144
145GetConnected ( ) ;
146
147// 对窗体中所需要的内容进行初始化
148
149InitializeComponent ( ) ;
150
151}
152
153file:// 清除在程序中使用过的资源
154
155protected override void Dispose( bool disposing )
156
157{
158
159if( disposing )
160
161{
162
163if ( components != null )
164
165{
166
167components.Dispose ( ) ;
168
169}
170
171}
172
173base.Dispose( disposing ) ;
174
175}
176
177public static void Main ( )
178
179{
180
181Application.Run ( new Data ( ) ) ;
182
183}
184
185public void GetConnected ( )
186
187{
188
189try
190
191{
192
193file:// 创建一个 OleDbConnection
194
195string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
196
197OleDbConnection myConn = new OleDbConnection ( strCon ) ;
198
199string strCom = " SELECT * FROM person " ;
200
201file:// 创建一个 DataSet
202
203myDataSet = new DataSet ( ) ;
204
205myConn.Open ( ) ;
206
207file:// 用 OleDbDataAdapter 得到一个数据集
208
209OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
210
211file:// 把 Dataset 绑定 books 数据表
212
213myCommand.Fill ( myDataSet , "person" ) ;
214
215file:// 关闭此 OleDbConnection
216
217myConn.Close ( ) ;
218
219}
220
221catch ( Exception e )
222
223{
224
225MessageBox.Show ( " 连接错误 ! " + e.ToString ( ) , " 错误 " ) ;
226
227}
228
229}
230
231private void InitializeComponent ( )
232
233{
234
235file:// 添加控件,略
236
237this.Name = "Data" ;
238
239this.Text = "Visual C #的数据库编程! " ;
240
241this.ResumeLayout(false) ;
242
243myBind = this.BindingContext [ myDataSet , "person" ] ;
244
245}
246
247protected void New_record ( object sender , System.EventArgs e )
248
249{
250
251t_id.Text = ( myBind.Count + 1 ).ToString ( ) ;
252
253t_xm.Text = "" ;
254
255t_xb.Text = "" ;
256
257t_nl.Text = "" ;
258
259t_books.Text = "" ;
260
261}
262
263protected void Insert_record ( object sender , System.EventArgs e )
264
265{
266
267try
268
269{
270
271file:// 判断所有字段是否添完,添完则执行,反之弹出提示
272
273if ( t_id.Text != "" && t_xm.Text != "" && t_xb.Text != "" && t_nl.Text != "" && t_books.Text != "" )
274
275{
276
277string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
278
279OleDbConnection myConn = new OleDbConnection ( myConn1 ) ;
280
281myConn.Open ( ) ;
282
283string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;
284
285strInsert += t_id.Text + ", '" ;
286
287strInsert += t_xm.Text + "', '" ;
288
289strInsert += t_xb.Text + "', " ;
290
291strInsert += t_nl.Text + ", " ;
292
293strInsert += t_books.Text + ")" ;
294
295OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
296
297inst.ExecuteNonQuery ( ) ;
298
299myConn.Close ( ) ;
300
301myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;
302
303myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;
304
305myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
306
307}
308
309else
310
311{
312
313MessageBox.Show ( " 必须填满所有字段值! " , " 错误! " ) ;
314
315}
316
317}
318
319catch ( Exception ed )
320
321{
322
323MessageBox.Show ( " 保存数据记录发生 " + ed.ToString ( ) , " 错误! " ) ;
324
325}
326
327}
328
329protected void Update_record ( object sender , System.EventArgs e )
330
331{
332
333int i = myBind.Position ;
334
335try{
336
337file:// 连接到一个数据库
338
339string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;
340
341OleDbConnection myConn = new OleDbConnection ( strCon ) ;
342
343myConn.Open ( ) ;
344
345myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . BeginEdit ( ) ;
346
347file:// 从数据库中修改指定记录
348
349string strUpdt = " UPDATE person SET xm = '"
350
351\+ t_xm.Text + "' , xb = '"
352
353\+ t_xb.Text + "' , nl = "
354
355\+ t_nl.Text + " , zip = "
356
357\+ t_books.Text + " WHERE id = " + t_id.Text ;
358
359OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;
360
361myCommand.ExecuteNonQuery ( ) ;
362
363myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . EndEdit ( ) ;
364
365myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
366
367myConn.Close ( ) ;
368
369}
370
371catch ( Exception ed )
372
373{
374
375MessageBox.Show ( " 修改指定记录错误: " + ed.ToString ( ) , " 错误! " ) ;
376
377}
378
379myBind.Position = i ;
380
381}
382
383protected void Delete_record ( object sender , System.EventArgs e )
384
385{
386
387DialogResult r = MessageBox.Show ( " 是否删除当前记录! " , " 删除当前记录! " , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ;
388
389int ss = ( int ) r ;
390
391if ( ss == 6 ) // 按动 " 确定 " 按钮
392
393{
394
395try{
396
397file:// 连接到一个数据库
398
399string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " ;
400
401OleDbConnection myConn = new OleDbConnection ( strCon ) ;
402
403myConn.Open ( ) ;
404
405string strDele = "DELETE FROM person WHERE id= " + t_id.Text ;
406
407OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;
408
409file:// 从数据库中删除指定记录
410
411myCommand.ExecuteNonQuery ( ) ;
412
413file:// 从 DataSet 中删除指定记录
414
415myDataSet.Tables [ "person" ] . Rows [ myBind.Position ] . Delete ( ) ;
416
417myDataSet.Tables [ "person" ] . AcceptChanges ( ) ;
418
419myConn.Close ( ) ;
420
421}
422
423catch ( Exception ed )
424
425{
426
427MessageBox.Show ( " 删除记录错误信息: " + ed.ToString ( ) , " 错误! " ) ;
428
429}
430
431}
432
433}
434
435file:// 按钮 " 尾记录 " 对象事件程序
436
437protected void GoLast ( object sender , System.EventArgs e )
438
439{
440
441myBind.Position = myBind.Count - 1 ;
442
443}
444
445file:// 按钮 " 下一条 " 对象事件程序
446
447protected void GoNext ( object sender , System.EventArgs e )
448
449{
450
451if ( myBind.Position == myBind.Count -1 )
452
453MessageBox.Show ( " 已经到了最后一条记录! ", " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
454
455else
456
457myBind.Position += 1 ;
458
459}
460
461file:// 按钮 " 上一条 " 对象事件程序
462
463protected void GoPrevious ( object sender , System.EventArgs e )
464
465{
466
467if ( myBind.Position == 0 )
468
469MessageBox.Show ( " 已经到了第一条记录! " , " 信息提示! " , MessageBoxButtons.OK , MessageBoxIcon.Information ) ;
470
471else
472
473myBind.Position -= 1 ;
474
475}
476
477file:// 按钮 " 首记录 " 对象事件程序
478
479protected void GoFirst ( object sender , System.EventArgs e )
480
481{
482
483myBind.Position = 0 ;
484
485}
486
487}
488
489对于以 Sql Server 2000 数据库为模型的程序代码,只要把 Data01.cs 中的数据链接,即:
490
491string myConn1 = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;
492
493改换成:
494
495string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
496
497注释:此数据链接代表的意思是:打开 Sql server 数据库,服务器名称为 server1, 数据库为 data1
498
499就可以得到 Visual C #针对 Sql Server 2000 数据库为模板编程的完成源程序代码了。所以本文就不再提供了。
500
501七.总结:
502
503数据库编程始终是程序编程内容中的一个重点和难点。而以上介绍的这些操作又是数据库编程中最为基本,也是最为重要的内容。那些复杂的编程无非是以上这些处理的若干个叠加。</p>