中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
VB.NET的数据库基础编程
作者:王天 时间:2006-09-29 11:22 出处:ccidnet.com 责编:月夜寒箫
              摘要:VB.NET的数据库基础编程

面向数据库编程始终是程序设计的一个难点和重点,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 = 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 ( )
            '创建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 = 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 ( )
            '创建BindingManagerBase对象
            myBind = Me.BindingContext  ( myDataSet  , "person" )     

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

            '按钮"尾记录"对象事件程序
            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 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.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   

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

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

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

            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   

在介绍完上面这二个典型的数据操作后,我们不难发现,其实对数据库进行编程,选取什么样的数据库类型对于程序开发并不是十分主要的,因为在用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 <> "" ) 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   

同样对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.Data
            Public 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 Sub
            End Class
            Module Module1
            Sub 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开发数据库相关程序有所帮助。

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有