|
TOP
|
| 文章内容 |
VB.Net是没有自己的类库的,他依托的类库是.Net FrameWork SDK中的类库,虽然在.Net FrameWrok SDK中并没有提供在Visual Basic中的的DbLabel、DbCombox等如此的数据库组件,但.Net FrameWork SDK中提供了一种数据绑定技术,可以把打开的数据表中的某个或者某些字段绑定到在命名空间System.Window.Forms中定义的WinForm组件(譬如:TextBox组件、ComBox组件、Label组件等)中的某些属性上,从而提供这些组件显示出数据表中的记录信息,也就实现了DbTextBox、DbCombox等组件。本文就是来探讨一下,如何在VB.Net中实现数据绑定。为了更清楚的说明问题,在讨论绑定的时候,在数据库的选择上,不仅选用了本地数据库Access 2000,而且也选用了远程数据库SQL Server 7.0。由于WinFrom组件比较多,但他们实现数据绑定的方法基本是一样的,在本文中,选用了三个比较常用的WinForm组件为代表,即:TextBox组件、ComBox组件和ListBox组件。
一.数据库的数据结构:
Access数据库名称为"Sample.mdb",里面定义了一张"books"的数据表,字段属性为下表:
| 字段名称 |
字段类型 |
代表意思 |
| Bookid |
数字 |
序号 |
| booktitle |
文本 |
书籍名称 |
| bookauthor |
文本 |
书籍作者 |
| bookprice |
数字 |
价格 |
| bookstock |
数字 |
书架号 |
Sql Server 7.0的数据库服务器名称为"server1",数据库名称为"data1",在此数据库中也有一张"books"表,字段的属性同上。
二.程序设计和运行的环境设置:
- 视窗2000服务器版
- Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )
- .Net FrameWrok SDK Beta 2版
三.对TextBox组件进行数据绑定:
在整个数据绑定中,对不同的组件可以大致分为二类,一类是简单型数据绑定,另外一种是复杂型数据绑定。简单型的数据针对的对象是TextBox组件、Label组件等,绑定后组件的显示的记录只有一条;而复杂型的针对对象是ListBox组件、TreeView组件等,往往绑定后显示出来的记录是比较多的。对于简单型的数据绑定可以参考本文中TextBox组件的绑定的过程。对于复杂型的绑定过程可以参考本文中的ComBox组件和ListBox组件的绑定过程。
- 其实无论是和中数据绑定,首先要打开指定的数据表,得到数据集。下面语句是打开Access数据库中的"books"表,得到"myDataSet"数据集:
'打开数据表,返回数据集 public Sub GetConnected ( ) '创建一个 OleDbConnection Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\sample.mdb" Dim myConn As OleDbConnection = new OleDbConnection ( ) myConn.ConnectionString = strCon
Dim strCom As string = " SELECT * FROM books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( ) End Sub |
- 实现数据绑定:
TextBox组件通过下列语句就可以把已经得到的数据集"myDataSet"中的"books.bookid"字段值绑定到TextBox1的"Text"属性上: TextBox1.DataBindings.Add ( New Binding ( "Text" , Me.myDataSet , "books.bookid" ) ) 了解了这二点,就不难实现对TextBox组件的数据绑定了。下面是实现对TextBox组件数据绑定的源程序代码(Text1.vb):
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private TextBox1 As TextBox Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '创建一个 OleDbConnection Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\sample.mdb" Dim myConn As OleDbConnection = new OleDbConnection ( ) myConn.ConnectionString = strCon
Dim strCom As string = " SELECT * FROM books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub '初始化窗体中的组件 Private Sub InitializeComponent ( ) Me.Text = "对TextBox组件实现数据绑定!" Me.Width = 400 Me.Height = 300
Button1 = New Button ( ) TextBox1 = New TextBox ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "数据绑定"
TextBox1.Left = 200 TextBox1.Top = 30 TextBox1.Width = 150 TextBox1.Height = 40
Me.Controls.Add ( Button1 ) Me.Controls.Add ( TextBox1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click TextBox1.DataBindings.Add ( New Binding ( "Text" , Me.myDataSet , "books.bookid" ) ) End Sub End Class
Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module | 经过了下列语句编译后:
vbc.exe /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:system.data.dll /r:system.xml.dll text1.vb | 可以得到如下界面:
 图01:对TextBox组件绑定后的程序界面
- 把Sql Server 7.0上的字段绑定到TextBox的"Text"的属性上:
有了上面的知识,可以非常方便的得出下列代码,和上面的代码的主要区别在于选用的数据库引擎不一样,由此如果你使用的是Oracle等数据库实现这种操作,也只需要改变数据连接就可以了,具体如下(Text2.vb):
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private TextBox1 As TextBox Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1 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 books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub '初始化窗体中的组件 Private Sub InitializeComponent ( ) Me.Text = "对TextBox组件实现数据绑定!" Me.Width = 400 Me.Height = 300 Button1 = New Button ( ) TextBox1 = New TextBox ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "数据绑定"
TextBox1.Left = 200 TextBox1.Top = 30 TextBox1.Width = 150 TextBox1.Height = 40
Me.Controls.Add ( Button1 ) Me.Controls.Add ( TextBox1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click TextBox1.DataBindings.Add ( New Binding ( "Text" , Me.myDataSet , "books.bookid" ) ) End Sub End Class
Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module |
- 对其他简单型的组件的数据绑定:
有了上面的这些代码,对其他也是简单型数据绑定组件,只需要把程序TextBox组件改为要绑定的组件就可以了。下面是把TextBox组件改为Label组件的程序代码(Label.vb)和编译好程序的运行界面:
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private Label1 As Label Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '创建一个 OleDbConnection Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\sample.mdb" Dim myConn As OleDbConnection = new OleDbConnection ( ) myConn.ConnectionString = strCon
Dim strCom As string = " SELECT * FROM books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub '初始化窗体中的组件 Private Sub InitializeComponent ( ) Me.Text = "对Label组件实现数据绑定!" Me.Width = 400 Me.Height = 300
Button1 = New Button ( ) Label1 = New Label ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "数据绑定"
Label1.Left = 200 Label1.Top = 30 Label1.Width = 150 Label1.Height = 40
Me.Controls.Add ( Button1 ) Me.Controls.Add ( Label1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click Label1.DataBindings.Add ( New Binding ( "Text" , Me.myDataSet , "books.bookid" ) ) End Sub End Class
Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module |
 图02:对Label组件绑定后的程序界面
四.对ComBox组件进行数据绑定:
上面介绍的是对组件的简单数据绑定,对组件的复杂数据绑定和它有所区别,也有所相同,具体如下:
- 要对ComBox组件实现数据绑定,首先也是要打开数据表,得到数据集。这和上面TextBox组件的代码大致一样,在此略过。
- 实现数据绑定:
设定了Combox组件的三个属性就可以实现数据绑定了,这三个属性是"DataSource"、"DisplayMember"、"ValueMember"。其中"DataSource"是指定的数据集;"DisplayMember"是ComBox组件显示的字段值;"ValueMember"是ComBox组件选择后的值。具体代码如下:
ComboBox1.DataSource = Me.myDataSet ComboBox1.DisplayMember = "books.booktitle" ComboBox1.ValueMember = "books.booktitle" 由此可以得到ComBox组件数据绑定的源程序代码(ComBox1.vb),具体如下: Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private ComboBox1 As ComboBox Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '创建一个 OleDbConnection Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\sample.mdb" Dim myConn As OleDbConnection = new OleDbConnection ( ) myConn.ConnectionString = strCon
Dim strCom As string = " SELECT * FROM books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub
Private Sub InitializeComponent ( ) Me.Text = "对ComBox组件实现数据绑定!" Me.Width = 400 Me.Height = 300
Button1 = New Button ( ) ComboBox1 = New ComboBox ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "按钮"
ComboBox1.Left =150 ComboBox1.Top =50
Me.Controls.Add ( Button1 ) Me.Controls.Add ( ComboBox1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click ComboBox1.DataSource = Me.myDataSet ComboBox1.DisplayMember = "books.booktitle" ComboBox1.ValueMember = "books.booktitle" End Sub
End Class
Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module
| 经过了下列语句编译后:
vbc.exe /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:system.data.dll /r:system.xml.dll combox1.vb | 运行程序可以得到如下界面:
 图03:对ComBox组件绑定后的程序界面
- ComBox组件对数据库Sql Server 7.0的字段进行数据绑定:
同样只需更好数据引擎就可以得到针对数据库Sql Server 7.0数据绑定的源代码,如下:
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private ComboBox1 As ComboBox Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1 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 books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub
Private Sub InitializeComponent ( ) Me.Text = "对ComBox组件实现数据绑定!" Me.Width = 400 Me.Height = 300
Button1 = New Button ( ) ComboBox1 = New ComboBox ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "按钮"
ComboBox1.Left =150 ComboBox1.Top =50
Me.Controls.Add ( Button1 ) Me.Controls.Add ( ComboBox1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click ComboBox1.DataSource = Me.myDataSet ComboBox1.DisplayMember = "books.booktitle" ComboBox1.ValueMember = "books.booktitle" End Sub
End Class Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module |
五.对ListBox组件进行数据绑定:
- 对ListBox组件的数据绑定和ComBox组件的数据绑定大致是相同的,也是对其的三个属性"DataSource"、"DisplayMember"、"ValueMember"进行设定。具体的过程可以参考一下上面ComBox组件的设定过程,下面是对ListBox组件数据绑定的源程序代码(ListBox1.vb):
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private ListBox1 As ListBox Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '创建一个 OleDbConnection Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ..\sample.mdb" Dim myConn As OleDbConnection = new OleDbConnection ( ) myConn.ConnectionString = strCon
Dim strCom As string = " SELECT * FROM books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub
Private Sub InitializeComponent ( ) Me.Text = "对ListBox组件实现数据绑定!" Me.Width = 400 Me.Height = 300
Button1 = New Button ( ) ListBox1 = New ListBox ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "数据绑定"
ListBox1.Left =150 ListBox1.Top =50
Me.Controls.Add ( Button1 ) Me.Controls.Add ( ListBox1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click ListBox1.DataSource = Me.myDataSet ListBox1.DisplayMember = "books.booktitle" ListBox1.ValueMember = "books.booktitle" End Sub
End Class
Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module | 经过了下列语句编译后:
vbc.exe /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:system.data.dll /r:system.xml.dll listbox1.vb | 可以得到如下运行界面:
 图04:对ListBox组件绑定后的程序界面
- ListBox组件对数据库Sql Server 7.0的字段进行数据绑定源程序代码(ListBox1.vb):
同样只需更好数据引擎就可以得到针对数据库Sql Server 7.0数据绑定的源代码,如下:
Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System Imports System.Data.OleDb Imports System.Data
Public Class Form1 Inherits Form
Private WithEvents Button1 As Button Private ListBox1 As ListBox Private myDataSet As DataSet Private components As System.ComponentModel.Container
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 ( ) '设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1 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 books " '创建一个 DataSet myDataSet = new DataSet( )
myConn.Open ( ) '用 OleDbDataAdapter 得到一个数据集 Dim myCommand As OleDbDataAdapter = new OleDbDataAdapter ( strCom , myConn ) '把Dataset绑定books数据表 myCommand.Fill ( myDataSet , "books" ) '关闭此OleDbConnection myConn.Close ( )
End Sub
Private Sub InitializeComponent ( ) Me.Text = "对ListBox组件实现数据绑定!" Me.Width = 400 Me.Height = 300
Button1 = New Button ( ) ListBox1 = New ListBox ( )
Button1.Left = 200 Button1.Top = 200 Button1.Width = 100 Button1.Height = 40 Button1.TabIndex = 0 Button1.Text = "数据绑定"
ListBox1.Left =150 ListBox1.Top =50
Me.Controls.Add ( Button1 ) Me.Controls.Add ( ListBox1 )
End Sub
Private Sub Button1_Click ( ByVal sender As Object , _ ByVal e As System.EventArgs ) Handles Button1.Click ListBox1.DataSource = Me.myDataSet ListBox1.DisplayMember = "books.booktitle" ListBox1.ValueMember = "books.booktitle" End Sub
End Class
Module Module1 Sub Main ( ) Application.Run ( new Form1 ( ) ) End sub End Module |
六.总结:
数据绑定技术是用.Net程序开发语言进行数据方面编程的基本,也是最为重要的环节。是对数据库中记录进行添加、删除、浏览、修改等操作的基础。虽然对于不同的WinForm组件,数据绑定的方法有所差别,但总体来说,还是大同小异的,只需要对上面的代码进行一定的修改就可以了。虽然对这些WinFrom组件已经绑定了数据,但这些组件显示出来的数据都是静止的,在下面的文章中,我们将依靠数据绑定为基础,实现数据记录的浏览、添加、删除、修改等操作,那就让我们下回见吧!
【 |
|
| 相关信息 |
![]() |
探讨VB.Net中的数据绑定
发布者:mmcbbs
浏览量:663
发布日期:2005-04-10 10:28:33
所属专题: |
|
|
|
|