七.VB.NET数据库基本编程完整源程序代码和程序运行界面:
在掌握了上面的那些针对数据库的基本操作过以后,就可以得到用VB.NET进行数据库基本编程的源程序代码,下面代码(data.vb)的操作数据库是Access 2000,如下:
Imports SystemImports System.DrawingImports System.ComponentModelImports System.Windows.FormsImports System.Data.OleDbImports System.DataPublic Class Form1 Inherits Form Private components As System.ComponentModel.Container = Nothing Private WithEvents lastrec As Button Private WithEvents nextrec As Button Private WithEvents previousrec As Button Private WithEvents firstrec As Button Private t_books As TextBox Private t_nl As TextBox Private t_xb As TextBox Private t_xm As TextBox Private t_id As TextBox Private l_books As Label Private l_nl As Label Private l_xb As Label Private l_xm As Label Private l_id As Label Private label1 As Label Private myDataSet As DataSet Private WithEvents button1 As Button Private WithEvents button2 As Button Private WithEvents button3 As Button Private WithEvents button4 As Button Private myBind As BindingManagerBase Public Sub New ( ) MyBase.New ( ) GetConnected ( ) InitializeComponent ( ) End Sub '清除在程序中使用过的资源 Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose ( ) End If End If MyBase.Dispose ( disposing ) End Sub Public Sub GetConnected ( ) '创建一个数据连接 Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " Dim myConn As OleDbConnection = New OleDbConnection ( ) myConn.ConnectionString = strCon Dim strCom As String = " SELECT * FROM person " '创建一个 DataSet myDataSet = New DataSet ( ) myConn.Open ( ) '通过OleDbDataAdapter对象得到一个数据集 Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "person" ) '关闭此数据连接 myConn.Close ( ) End Sub '初始化窗体中的组件 Private Sub InitializeComponent ( ) label1 = New Label ( ) l_xm = New Label ( ) l_books = New Label ( ) t_xm = New TextBox ( ) t_nl = New TextBox ( ) nextrec = New Button ( ) lastrec = New Button ( ) firstrec = New Button ( ) button1 = New Button ( ) t_xb = New TextBox ( ) button3 = New Button ( ) button4 = New Button ( ) t_books = New TextBox ( ) previousrec = New Button ( ) button2 = New Button ( ) l_nl = New Label ( ) l_xb = New Label ( ) l_id = New Label ( ) t_id = New TextBox ( ) SuspendLayout ( ) label1.Font = New Font ( "Microsoft Sans Serif" , 14! ) label1.ForeColor = Color.Navy label1.Location = New Point ( 40 , 24 ) label1.Name = "label1" label1.Size = New Size ( 265 , 28 ) label1.TabIndex = 19 label1.Text = "VB.NET数据库基础编程" label1.TextAlign = ContentAlignment.MiddleCenter l_xm.Font = New Font ( "Microsoft Sans Serif" , 10! ) l_xm.Location = New Point ( 40 , 104 ) l_xm.Name = "l_xm" l_xm.Size = New Size ( 143 , 23 ) l_xm.TabIndex = 14 l_xm.Text = "姓 名:" l_xm.TextAlign = ContentAlignment.MiddleCenter l_books.Font = New Font ( "Microsoft Sans Serif" , 10! ) l_books.Location = New Point ( 40 , 200 ) l_books.Name = "l_books" l_books.Size = New Size ( 143 , 24 ) l_books.TabIndex = 16 l_books.Text = "邮政编码:" l_books.TextAlign = ContentAlignment.MiddleCenter t_xm.Location = New Point ( 160 , 104 ) t_xm.Name = "t_xm" t_xm.Size = New Size ( 104 , 21 ) t_xm.TabIndex = 2 t_xm.Text = "" t_xm.DataBindings.Add ( New Binding ( "Text" , myDataSet , "person.xm" ) ) t_nl.Location = New Point ( 160 , 168 ) t_nl.Name = "t_nl" t_nl.Size = New Size ( 102 , 21 ) t_nl.TabIndex = 4 t_nl.Text = "" t_nl.DataBindings.Add ( New Binding ( "Text" , myDataSet , "person.nl" ) ) nextrec.Font = New Font ( "Microsoft Sans Serif" , 8! ) nextrec.ForeColor = Color.Black nextrec.Location = New Point ( 176 , 248 ) nextrec.Name = "nextrec" nextrec.Size = New Size ( 60 , 28 ) nextrec.TabIndex = 8 nextrec.Text = "后一条" lastrec.Font = New Font ( "Microsoft Sans Serif" , 8! ) lastrec.ForeColor = Color.Black lastrec.Location = New Point ( 240 , 248 ) lastrec.Name = "lastrec" lastrec.Size = New Size ( 60 , 28 ) lastrec.TabIndex = 9 lastrec.Text = "尾记录" firstrec.Font = New Font ( "Microsoft Sans Serif" , 8! ) firstrec.ForeColor = Color.Black firstrec.Location = New Point ( 48 , 248 ) firstrec.Name = "firstrec" firstrec.Size = New Size ( 60 , 28 ) firstrec.TabIndex = 6 firstrec.Text = "首记录" button1.Font = New Font ( "Microsoft Sans Serif" , 8! ) button1.ForeColor = Color.Black button1.Location = New Point ( 16 , 296 ) button1.Name = "button1" button1.Size = New Size ( 70 , 28 ) button1.TabIndex = 10 button1.Text = "新建记录" t_xb.Location = New Point ( 160 , 136 ) t_xb.Name = "t_xb" t_xb.Size = New Size ( 104 , 21 ) t_xb.TabIndex = 3 t_xb.Text = "" t_xb.DataBindings.Add ( New Binding ( "Text" , myDataSet , "person.xb" ) ) button3.Font = New Font ( "Microsoft Sans Serif" , 8! ) button3.ForeColor = Color.Black button3.Location = New Point ( 176 , 296 ) button3.Name = "button3" button3.Size = New Size ( 70 , 28 ) button3.TabIndex = 12 button3.Text = "修改记录" button4.Font = New Font ( "Microsoft Sans Serif" , 8! ) button4.ForeColor = Color.Black button4.Location = New Point ( 256 , 296 ) button4.Name = "button4" button4.Size = New Size ( 70 , 28 ) button4.TabIndex = 13 button4.Text = "删除记录" t_books.Location = New Point ( 160 , 200 ) t_books.Name = "t_books" t_books.Size = New Size ( 102 , 21 ) t_books.TabIndex = 5 t_books.Text = "" t_books.DataBindings.Add ( New Binding ( "Text" , myDataSet , "person.zip" ) ) previousrec.Font = New Font ( "Microsoft Sans Serif" , 8! ) previousrec.ForeColor = Color.Black previousrec.Location = New Point ( 112 , 248 ) previousrec.Name = "previousrec" previousrec.Size = New Size ( 60 , 28 ) previousrec.TabIndex = 7 previousrec.Text = "上一条" button2.Font = New Font ( "Microsoft Sans Serif" , 8! ) button2.ForeColor = Color.Black button2.Location = New Point ( 96 , 296 ) button2.Name = "button2" button2.Size = New Size ( 70 , 28 ) button2.TabIndex = 11 button2.Text = "插入记录" l_nl.Font = New Font ( "Microsoft Sans Serif" , 10! ) l_nl.Location = New Point ( 40 , 168 ) l_nl.Name = "l_nl" l_nl.Size = New Size ( 143 , 23 ) l_nl.TabIndex = 4 l_nl.Text = "年 龄:" l_nl.TextAlign = ContentAlignment.MiddleCenter l_xb.Font = New Font ( "Microsoft Sans Serif" , 10! ) l_xb.Location = New Point ( 40 , 136 ) l_xb.Name = "l_xb" l_xb.Size = New Size ( 143 , 23 ) l_xb.TabIndex = 17 l_xb.Text = "姓 别:" l_xb.TextAlign = ContentAlignment.MiddleCenter l_id.Font = New Font ( "Microsoft Sans Serif" , 10! ) l_id.Location = New Point ( 40 , 72 ) l_id.Name = "l_id" l_id.Size = New Size ( 143 , 23 ) l_id.TabIndex = 13 l_id.Text = "序 号:" l_id.TextAlign = ContentAlignment.MiddleCenter t_id.Enabled = False t_id.Location = New Point ( 160 , 72 ) t_id.Name = "t_id" t_id.Size = New Size ( 102 , 21 ) t_id.TabIndex = 1 t_id.Text = "" t_id.DataBindings.Add ( New Binding ( "Text" , myDataSet , "person.id" ) ) Me.AutoScaleBaseSize = New Size ( 6 , 14 ) Me.ClientSize = New Size ( 344 , 357 ) '在窗体中加入相应的组件 Me.Controls.Add ( button4 ) Me.Controls.Add ( button3 ) Me.Controls.Add ( button2 ) Me.Controls.Add ( button1 ) Me.Controls.Add ( lastrec ) Me.Controls.Add ( nextrec ) Me.Controls.Add ( previousrec ) Me.Controls.Add ( firstrec ) Me.Controls.Add ( t_books ) Me.Controls.Add ( t_nl ) Me.Controls.Add ( t_xb ) Me.Controls.Add ( t_xm ) Me.Controls.Add ( t_id ) Me.Controls.Add ( l_books ) Me.Controls.Add ( l_nl ) Me.Controls.Add ( l_xb ) Me.Controls.Add ( l_xm ) Me.Controls.Add ( l_id ) Me.Controls.Add ( label1 ) Me.Name = "Form1" Me.MaximizeBox = False Me.MinimizeBox = False Me.Text = "VB.NET数据库基础编程!" Me.ResumeLayout ( False ) '创建BindingManagerBase对象 myBind = Me.BindingContext ( myDataSet , "person" ) End Sub Private Sub button1_Click (ByVal sender As Object , _ ByVal e As System.EventArgs) Handles button1.Click t_id.Text = ( myBind.Count + 1 ).ToString ( ) t_xm.Text = "" t_xb.Text = "" t_nl.Text = "" t_books.Text = "" End Sub '插入数据记录操作代码 Private Sub button2_Click (ByVal sender As Object , _ ByVal e As System.EventArgs) Handles button2.Click '判断所有字段是否添完,添完则执行,反之弹出提示 If ( t_id.Text <> "" And t_xm.Text <> "" And t_xb.Text <> "" And t_nl.Text <> "" And t_books.Text <> "" ) Then Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 ) myConn.Open ( ) Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " & _ t_id.Text + " , '" & _ t_xm.Text + "' , '" & _ t_xb.Text + "' , " & _ t_nl.Text + " , " & _ t_books.Text + ")" Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn ) inst.ExecuteNonQuery ( ) myConn.Close ( ) myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( ) myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( ) myDataSet.Tables ( "person" ).AcceptChanges ( ) Else MessageBox.Show ( "必须填满所有字段值!" , "错误!" ) End If End Sub '修改数据记录代码 Private Sub button3_Click (ByVal sender As Object , _ ByVal e As System.EventArgs) Handles button3.Click Dim i As Integer = myBind.Position '连接到一个数据库 Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " Dim myConn As OleDbConnection = New OleDbConnection ( strCon ) myConn.Open ( ) myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( ) '从数据库中修改指定记录 Dim strUpdt As String = " UPDATE person SET xm = '" _ + t_xm.Text + "' , xb = '" _ + t_xb.Text + "' , nl = " _ + t_nl.Text + " , zip = " _ + t_books.Text + " WHERE id = " + t_id.Text Dim myCommand As OleDbCommand = New OleDbCommand ( strUpdt , myConn ) myCommand.ExecuteNonQuery ( ) myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( ) myDataSet.Tables ( "person" ).AcceptChanges ( ) myConn.Close ( ) myBind.Position = i End Sub '删除数据记录代码 Private Sub button4_Click (ByVal sender As Object , _ ByVal e As System.EventArgs) Handles button4.Click '连接到一个数据库 Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb " Dim myConn As OleDbConnection = New OleDbConnection ( strCon ) myConn.Open ( ) Dim strDele As String = "DELETE FROM person WHERE id= " + t_id.Text Dim myCommand As OleDbCommand = New OleDbCommand ( strDele , myConn ) '从数据库中删除指定记录 myCommand.ExecuteNonQuery ( ) '从DataSet中删除指定记录 myDataSet.Tables ( "person" ).Rows ( myBind.Position ).Delete ( ) myDataSet.Tables ( "person" ).AcceptChanges ( ) myConn.Close ( ) End Sub '按钮"尾记录"对象事件程序 Private Sub lastrec_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles lastrec.Click myBind.Position = myBind.Count - 1 End Sub '按钮"下一条"对象事件程序 Private Sub nextrec_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles nextrec.Click If myBind.Position = myBind.Count - 1 Then MessageBox.Show ( "已经到了最后一条记录!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information ) Else myBind.Position = myBind.Position + 1 End If End Sub '按钮"上一条"对象事件程序 Private Sub previousrec_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles previousrec.Click If ( myBind.Position = 0 ) Then MessageBox.Show ( "已经到了第一条记录!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information ) Else myBind.Position = myBind.Position - 1 End If End Sub '按钮"首记录"对象事件程序 Private Sub firstrec_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles firstrec.Click myBind.Position = 0 End SubEnd ClassModule Module1 Sub Main ( ) Application.Run ( New Form1 ( ) ) End SubEnd Module
经过以下编译命令编译过以后:
vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:system.xml.dll /r:system.data.dll data.vb
运行程序就可以得到下面的运行界面:
![](http://img.win8xp.cn/technique_6/technique_0121093149_1.jpg)
图01:VB.NET数据库基础编程运行界面
八:总结:
VB.NET进行数据库编程主要利用的是ADO.NET,ADO.NET是.Net FrameWork SDK中用以操作数据库的一系列类库的总称。本文介绍的这些操作虽然是最为基本的,但其实也是至关重要的,因为任何一种复杂的数据库处理都可以分解为上述这些操作,希望本文介绍的这些内容对你用VB.NET开发数据库相关程序有所帮助。(出处:赛迪网)
|