http://www.51dd.com/infoview/Article_5625.html
面向数据库编程始终是程序设计的一个难点和重点,VB.NET和C#一样自身是不具备对数据库进行操作的功能,他们对数据库的处理是通过.Net FrameWork SDK中面向数据库编程的类库和微软的MDAC来实现的。在上一篇文章《探讨VB.Net中的数据绑定》中,我们已经探讨了数据绑定技术,这对于我们下面进行数据库编程是非常重要的。由于数据库编程中所包含的内容十分丰富,这是一篇文章所难以包容的。本文就来探讨一下用VB.NET进行数据库的基础编程,即:用VB.NET如何实现对数据的浏览,如何添加、插入记录,如何删除记录和如何更改记录。

一.程序设计和运行的环境设置:

(1).视窗2000服务器版

(2).Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )

(3)..Net FrameWork SDK Beta 2

二.数据库的数据字典:

为了更全面的介绍,在数据库的选取方面,选取了二种典型的数据库,其一是本地数据库,也就是本文主要介绍的的数据库Access 2000;另外一个是远程数据库SQL Server 2000。其中Access 2000的数据库名称是"db.mdb",在此数据库中只存放了一张数据表"person",此数据表结构如下:

字段名称 字段类型 字段意思
id 数字 序号
xm 文本 姓名
xb 文本 性别
nl 文本 年龄
zip 文本 邮政编码

远程数据库Sql Server 2000的数据库服务器名称为"Server1",数据库名称为"Data1",登陆的ID为"sa",口令为空,在数据库也只存放了一张"person"数据表,数据结构大致如上。

三.VB.NET如何实现对数据记录的浏览:

在完成对窗体中的WinForm组件进行绑定过以后,实现对数据记录的浏览操作的关键就是要找到如何定位数据记录指针的方法。而要实现这种处理就需要用到.Net FrameWork SDK中的名称空间System.Windows.Froms中的BindingManagerBase类了,BindingManagerBase是一个抽象的类,他主要管理对于绑定同一数据表所有绑定对象。BindingManagerBase类中定义了二个属性"position"和"Count",第一个属性是定义当前数据指针,而第二个属性主要是得到当前数据集有多少记录数目。在已经进行完数据绑定后,通过这二个属性的配合使用,实现对数据记录的浏览。那么如何创建一个属于自己的BindingManagerBase对象,这就要使用到另外一个类--BindingContext。其实对于那些属于从Control类中继承对象的BindingManagerBase都是由BindingContext来创建的,下面以Access 2000为操作数据库,创建的一个名称为"myBind"的BindingManagerBase对象的具体例子。

     '创建一个数据连接Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb "Dim myConn As OleDbConnection = New OleDbConnection ( ) myConn.ConnectionString = strConDim strCom As String = " SELECT * FROM person "'创建一个 DataSetmyDataSet = New DataSet ( ) myConn.Open ( ) '通过OleDbDataAdapter对象得到一个数据集Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter ( strCom  , myConn )'把Dataset绑定books数据表myCommand.Fill ( myDataSet  , "person" )'关闭此数据连接myConn.Close ( )'创建BindingManagerBase对象myBind = Me.BindingContext  ( myDataSet  , "person" )     

对于SQL Server数据库,创建BindingManagerBase对象和Access 2000大致相同,唯一不同的就在于创建数据连接的时候,下面是以SQL Server 2000为操作数据库,数据库服务器名称为"Server1",数据库名称为"Data1",登陆的ID为"sa",口令为空,在数据库也只存放了一张"person"数据表,创建BindingManagerBase对象的程序代码:

     '创建一个数据连接Dim strCon As String = " Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 "Dim myConn As OleDbConnection = New OleDbConnection ( ) myConn.ConnectionString = strConDim strCom As String = " SELECT * FROM person "'创建一个 DataSetmyDataSet = New DataSet ( ) myConn.Open ( ) '通过OleDbDataAdapter对象得到一个数据集Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter ( strCom  ,myConn )'把Dataset绑定books数据表myCommand.Fill ( myDataSet  , "person" )'关闭此数据连接myConn.Close ( )'创建BindingManagerBase对象myBind = Me.BindingContext  ( myDataSet  , "person" )     

在得到BindingManagerBase对象后,配合使用"position"属性和"Count"属性,就可以实现对数据集的浏览了,下面是对数据集进行"上一条"、"下一条"、"尾记录"、"首记录"。

     '按钮"尾记录"对象事件程序 Private Sub lastrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles lastrec.ClickmyBind.Position = myBind.Count - 1End Sub'按钮"下一条"对象事件程序 Private Sub nextrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles nextrec.ClickIf myBind.Position = myBind.Count - 1 ThenMessageBox.Show ( "已经到了最后一条记录!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information )ElsemyBind.Position = myBind.Position + 1End IfEnd Sub'按钮"上一条"对象事件程序 Private Sub previousrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles previousrec.ClickIf  ( myBind.Position = 0 ) ThenMessageBox.Show ( "已经到了第一条记录!" , "信息提示!" ,MessageBoxButtons.OK , MessageBoxIcon.Information )ElsemyBind.Position = myBind.Position - 1End IfEnd Sub'按钮"首记录"对象事件程序Private Sub firstrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles firstrec.ClickmyBind.Position = 0End Sub   

四.VB.NET来删除数据记录:

在做程序的时候,我们可能有这样的迷惑就是,我们操作的数据集是返回的DataSet对象,如果此时的DataSet对象十分庞大,或者连接到此数据库服务器的客户非常多,这样就会耗费服务器的很多资源,久而久之服务器总有一天可能会崩溃。其实这种担心是没有必要的,因为我们操作的DataSet对象其实产生的位置并不在服务器端,而是客户端,这样上面的几种顾虑就显得没有必要了。但在对数据库进行删除、修改等操作,我们此时操作的对象是服务器端的数据库,并没有修改到本地的DataSet对象,所以当进行删除、修改操作的时候,为了数据一致,在对服务器端的数据库进行删除、修改后,依然要对本地的DataSet对象进行相关操作。根据上面的这些知识,就可以分别得到针对Access 2000和Sql Server 2000数据库进行删除操作的程序代码:

< I > .删除Access 2000数据库中的记录:

     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.TextDim 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   

< II > .删除Sql Server 2000数据库中的记录:

     Private Sub button4_Click (ByVal sender As Object , _ByVal e As System.EventArgs) Handles button4.Click'连接到一个数据库Dim strCon As String = " Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 "Dim myConn As OleDbConnection = New OleDbConnection ( strCon )myConn.Open ( ) Dim strDele As String = "DELETE FROM person WHERE id= " + t_id.TextDim 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   

五.VB.NET来修改数据记录:

掌握了SQL语句,并且掌握了上面删除数据记录的实现过程过以后,用VB.NET来修改数据记录就显得并不十分困难了。下面就是以Access 2000为操作数据库实现记录修改的代码,如下:

     Private Sub button3_Click (ByVal sender As Object , _ByVal e As System.EventArgs) Handles button3.ClickDim 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.TextDim myCommand As OleDbCommand = New OleDbCommand ( strUpdt , myConn )myCommand.ExecuteNonQuery ( ) myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( ) myDataSet.Tables ( "person" ).AcceptChanges ( ) myConn.Close ( ) myBind.Position = iEnd Sub   

在介绍完上面这二个典型的数据操作后,我们不难发现,其实对数据库进行编程,选取什么样的数据库类型对于程序开发并不是十分主要的,因为在用VB.NET进行数据库开发的时候,对不同数据库,其开发代码的主要区别只在于数据链接的不同,所以在此就不单独介绍用VB.NET对Sql Server 2000进行修改记录操作的代码了。我想这应该不是很难吧。

六.VB.NET来插入数据记录:

往数据库中插入记录和修改记录和删除记录基本上差不多,主要也是利用SQL语句,下面是以Access 2000为操作数据库,进行插入记录的代码:

     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 <> "" ) ThenDim 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 ( ) ElseMessageBox.Show ( "必须填满所有字段值!" , "错误!" )End If
End Sub   

同样对Sql Server 2000数据库进行插入记录操作和Access 2000数据库插入记录操作的差异也只在于不同的数据链接,具体的代码可以参考"删除数据记录"中的代码,在这里就不提供了。

七.VB.NET数据库基本编程完整源程序代码和程序运行界面:

在掌握了上面的那些针对数据库的基本操作过以后,就可以得到用VB.NET进行数据库基本编程的源程序代码,下面代码(data.vb)的操作数据库是Access 2000,如下:

Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.DataPublic Class Form1Inherits FormPrivate components As System.ComponentModel.Container = NothingPrivate WithEvents lastrec As ButtonPrivate WithEvents nextrec As ButtonPrivate WithEvents previousrec As ButtonPrivate WithEvents firstrec As ButtonPrivate t_books As TextBoxPrivate t_nl As TextBoxPrivate t_xb As TextBoxPrivate t_xm As TextBoxPrivate t_id As TextBoxPrivate l_books As LabelPrivate l_nl As LabelPrivate l_xb As LabelPrivate l_xm As LabelPrivate l_id As LabelPrivate label1 As LabelPrivate myDataSet As DataSetPrivate WithEvents button1 As ButtonPrivate WithEvents button2 As ButtonPrivate WithEvents button3 As ButtonPrivate WithEvents button4 As ButtonPrivate myBind As BindingManagerBasePublic Sub New ( ) MyBase.New ( ) GetConnected ( ) InitializeComponent ( ) End Sub'清除在程序中使用过的资源 Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)If disposing ThenIf Not  (components Is Nothing) Thencomponents.Dispose ( ) End IfEnd IfMyBase.Dispose ( disposing )End SubPublic Sub GetConnected ( ) '创建一个数据连接Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb "Dim myConn As OleDbConnection = New OleDbConnection ( ) myConn.ConnectionString = strConDim strCom As String = " SELECT * FROM person "'创建一个 DataSetmyDataSet = 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.Navylabel1.Location = New Point ( 40 , 24 )label1.Name = "label1"label1.Size = New Size ( 265 , 28 )label1.TabIndex = 19label1.Text = "VB.NET数据库基础编程"label1.TextAlign = ContentAlignment.MiddleCenterl_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 = 14l_xm.Text = "姓      名:"l_xm.TextAlign = ContentAlignment.MiddleCenterl_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 = 16l_books.Text = "邮政编码:"l_books.TextAlign = ContentAlignment.MiddleCentert_xm.Location = New Point ( 160 , 104 )t_xm.Name = "t_xm"t_xm.Size = New Size ( 104 , 21 )t_xm.TabIndex = 2t_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 = 4t_nl.Text = ""t_nl.DataBindings.Add  ( New Binding  ( "Text"  , myDataSet  , "person.nl" ) ) nextrec.Font = New Font ( "Microsoft Sans Serif" , 8! )nextrec.ForeColor = Color.Blacknextrec.Location = New Point ( 176 , 248 )nextrec.Name = "nextrec"nextrec.Size = New Size ( 60 , 28 )nextrec.TabIndex = 8nextrec.Text = "后一条"lastrec.Font = New Font ( "Microsoft Sans Serif" , 8! )lastrec.ForeColor = Color.Blacklastrec.Location = New Point ( 240 , 248 )lastrec.Name = "lastrec"lastrec.Size = New Size ( 60 , 28 )lastrec.TabIndex = 9lastrec.Text = "尾记录"firstrec.Font = New Font ( "Microsoft Sans Serif" , 8! )firstrec.ForeColor = Color.Blackfirstrec.Location = New Point ( 48 , 248 )firstrec.Name = "firstrec"firstrec.Size = New Size ( 60 , 28 )firstrec.TabIndex = 6firstrec.Text = "首记录"button1.Font = New Font ( "Microsoft Sans Serif" , 8! )button1.ForeColor = Color.Blackbutton1.Location = New Point ( 16 , 296 )button1.Name = "button1"button1.Size = New Size ( 70 , 28 )button1.TabIndex = 10button1.Text = "新建记录"t_xb.Location = New Point ( 160 , 136 )t_xb.Name = "t_xb"t_xb.Size = New Size ( 104 , 21 )t_xb.TabIndex = 3t_xb.Text = ""t_xb.DataBindings.Add  ( New Binding  ( "Text"  , myDataSet  , "person.xb" ) ) button3.Font = New Font ( "Microsoft Sans Serif" , 8! )button3.ForeColor = Color.Blackbutton3.Location = New Point ( 176 , 296 ) button3.Name = "button3"button3.Size = New Size ( 70 , 28 )button3.TabIndex = 12button3.Text = "修改记录"button4.Font = New Font ( "Microsoft Sans Serif" , 8! )button4.ForeColor = Color.Blackbutton4.Location = New Point ( 256 , 296 )button4.Name = "button4"button4.Size = New Size ( 70 , 28 )button4.TabIndex = 13button4.Text = "删除记录"t_books.Location = New Point ( 160 , 200 )t_books.Name = "t_books"t_books.Size = New Size ( 102 , 21 )t_books.TabIndex = 5t_books.Text = ""t_books.DataBindings.Add  ( New Binding  ( "Text"  , myDataSet  , "person.zip" ) ) previousrec.Font = New Font ( "Microsoft Sans Serif" , 8! )previousrec.ForeColor = Color.Blackpreviousrec.Location = New Point ( 112 , 248 )previousrec.Name = "previousrec"previousrec.Size = New Size ( 60 , 28 )previousrec.TabIndex = 7previousrec.Text = "上一条"button2.Font = New Font ( "Microsoft Sans Serif" , 8! )button2.ForeColor = Color.Blackbutton2.Location = New Point ( 96 , 296 )button2.Name = "button2"button2.Size = New Size ( 70 , 28 )button2.TabIndex = 11button2.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 = 4l_nl.Text = "年      龄:"l_nl.TextAlign = ContentAlignment.MiddleCenterl_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 = 17l_xb.Text = "姓      别:"l_xb.TextAlign = ContentAlignment.MiddleCenterl_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 = 13l_id.Text = "序      号:"l_id.TextAlign = ContentAlignment.MiddleCentert_id.Enabled = Falset_id.Location = New Point ( 160 , 72 )t_id.Name = "t_id"t_id.Size = New Size ( 102 , 21 )t_id.TabIndex = 1t_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 = FalseMe.MinimizeBox = FalseMe.Text = "VB.NET数据库基础编程!"Me.ResumeLayout ( False )'创建BindingManagerBase对象myBind = Me.BindingContext  ( myDataSet  , "person" )  End SubPrivate Sub button1_Click (ByVal sender As Object , _ByVal e As System.EventArgs) Handles button1.Clickt_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 <> "" ) ThenDim 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 ( ) ElseMessageBox.Show ( "必须填满所有字段值!" , "错误!" )End IfEnd Sub'修改数据记录代码Private Sub button3_Click (ByVal sender As Object , _ByVal e As System.EventArgs) Handles button3.ClickDim 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.TextDim myCommand As OleDbCommand = New OleDbCommand ( strUpdt , myConn )myCommand.ExecuteNonQuery ( ) myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( ) myDataSet.Tables ( "person" ).AcceptChanges ( ) myConn.Close ( ) myBind.Position = iEnd 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.TextDim 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.ClickmyBind.Position = myBind.Count - 1End Sub'按钮"下一条"对象事件程序 Private Sub nextrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles nextrec.ClickIf myBind.Position = myBind.Count - 1 ThenMessageBox.Show ( "已经到了最后一条记录!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information )ElsemyBind.Position = myBind.Position + 1End IfEnd Sub'按钮"上一条"对象事件程序 Private Sub previousrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles previousrec.ClickIf  ( myBind.Position = 0 ) ThenMessageBox.Show ( "已经到了第一条记录!" , "信息提示!" , MessageBoxButtons.OK , MessageBoxIcon.Information )ElsemyBind.Position = myBind.Position - 1End IfEnd Sub'按钮"首记录"对象事件程序Private Sub firstrec_Click ( ByVal sender As Object , _ByVal e As System.EventArgs ) Handles firstrec.ClickmyBind.Position = 0End Sub
End ClassModule Module1Sub Main ( ) Application.Run ( New Form1 ( ) )End Sub
End Module   

经过以下编译命令编译过以后:

vbc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:system.xml.dll /r:system.data.dll data.vb   

运行程序就可以得到下面的运行界面:

图01:VB.NET数据库基础编程运行界面

八:总结:

VB.NET进行数据库编程主要利用的是ADO.NET,ADO.NET是.Net FrameWork SDK中用以操作数据库的一系列类库的总称。本文介绍的这些操作虽然是最为基本的,但其实也是至关重要的,因为任何一种复杂的数据库处理都可以分解为上述这些操作,希望本文介绍的这些内容对你用VB.NET开发数据库相关程序有所帮助。

转载于:https://www.cnblogs.com/penboy/archive/2005/04/24/144535.html

VB.NET的数据库基础编程[zz]相关推荐

  1. SQL Server 数据库基础编程

    Ø 判断表或其他对象及列是否存在 --判断某个表或对象是否存在 if (exists (select * from sys.objects where name = 'classes')) print ...

  2. 实验四 数据库SQL语言基础编程

    -- 实验四 数据库SQL语言基础编程 -- 实验目的: --  掌握数据库查询语句的编写方法 --  掌握利用查询语言完成基本查询 --  掌握利用SQL语句完成数据的添加.删除.修改操作 -- 实 ...

  3. day10-并发编程数据库基础

    Day10 并发编程&数据库基础 1 内容回顾 # 网络编程# 1.概念# osi五层协议# 应用层# 传输层# tcp 面向连接通信,并且数据之间无边界,可靠# 先建立连接 : 三次握手# ...

  4. Oracle数据库基础教程

    查看书籍详细信息: Oracle数据库基础教程 编辑推荐 体现作者多年的数据库管理与开发经验,结合大量实用技巧,重点突出,便于灵活掌握,提供典型应用实例与上机实验,分析详细,实用性强. 本书是作者结合 ...

  5. 零基础编程者初学python须知

    零基础编程者初学python须知 黄老师提示大家树立一个观念:通过学习python学会编程,不是简单学习python的而已. 自学有压力的建议参加" python培训_python从零基础到 ...

  6. 悟透delphi 第十一章 面向对象数据库基础

    第十一章 面向对象数据库基础 第二节数据对象的标识 我们在关系数据库的设计和开发中,可能经常需要一些唯一的编号或标识,用来作为关键字,以区别每一个不同的人,每一张不同的单据,每一次不同的信息登记,等等 ...

  7. python在线编程免费课程-Python少儿基础编程课程

    Python基础编程 L5-L8 主要内容: 为了帮孩子打下坚实编程基础,妙小程设计Python基础课程,学习Python基础知识及相关数学.物理等知识,并将其运用在游戏作品制作中.并让孩子了解并掌握 ...

  8. (超详细)MapReduce工作原理及基础编程

    MapReduce工作原理及基础编程(代码见文章后半部分) JunLeon--go big or go home 目录 MapReduce工作原理及基础编程(代码见文章后半部分) 一.MapReduc ...

  9. SQL基础编程——介绍及基本语法了解

    SQL基础编程 什么是SQL 结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理 ...

  10. C#基础编程——简介及基础语法

    C#基础编程--简介及基础语法 百科介绍 C#是微软公司发布的一种由C和C++衍生出来的面向对象的编程语言.运行于.NET Framework和.NET Core(完全开源,跨平台)之上的高级程序设计 ...

最新文章

  1. 学校SOLARIS ORACLE很好的电子教材
  2. flask貌似html文件里只能用flask指定的路径格式,css文件里则可用相对路径
  3. C++类的内存地址存放问题
  4. 在新项目中使用 Vue3 使用总结
  5. 7-87 吉老师的回归 (15 分)
  6. 改2003远程端口3389的方法!
  7. 从ELK到EFK演进
  8. 低档显卡无法支持2K显示器
  9. 必装 6 款超神的 GitHub 插件
  10. 遥感数据产品分级体系
  11. SCORM的对手——LOM
  12. GB28181 PS流传输格式详解
  13. html 刷新表格数据,当我刷新页面时在html表格上重复数据
  14. 三相电检测电路c语言,三相缺相检测电路的原理分析
  15. Leetcode_Weekly_310
  16. matlab读int16读文件_matlab读文件
  17. oracle 反斜杠 /
  18. Android Room提示 错误: Not sure how to convert a Cursor to this method's return type的原因及解决办法
  19. 自定义配置log日志
  20. 第二周铁人战队学习总结

热门文章

  1. 如何使用Wondershare PDFelement制作PDF文件
  2. Stack Overflow首席大神,他回答了超过3万个问题
  3. 7个示例科普CPU CACHE(zz)
  4. java泛型好处及案例
  5. c#调用c++ dll的一个例子
  6. redis学习笔记---java操作redis,使用expire模拟指定时间段内限制ip访问的次数;
  7. 【转】【开源专访】谢宝友:会说话的Linux内核
  8. PHP学习笔记【13】_正则表达式
  9. C语言深入浅出可变参数函数的使用技巧(转)
  10. Silverlight 图片路径问题