2)代码实现:
'指定程序中引用的名称空间:在代码窗中输入:
Imports System.Drawing
Imports System.Windows.Forms
Imports
Imports System
Imports System.Data.OleDb
Imports System.Data
'首先定义全局变量myDataSet及GetConnected()方法,代码如下:
Public Class Form1
Inherits Form
Private WithEvents Button1 As Button
Private TextBox1 As TextBox
……(省略部分自动生成代码)
Private myDataSet As DataSet
Private components As
Public Sub New()
MyBase.New()
GetConnected() '这个过程是用于建立连接,打开数据库
InitializeComponent()
End Sub
'清除在程序中使用过的资源
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
……(省略部分自动生成代码)
End Sub
'
'为GetConnected()方法增加代码,用来打开数据表,返回数据集。
Public Sub GetConnected()
'创建一个 OleDbConnection
Dim ConnectionString As String = ″ Provider = Microsoft.Jet.OLEDB.4.0; Data Source = e:\My Documents\db1.mdb″
Dim myConn As OleDbConnection = New OleDbConnection()
myConn.ConnectionString = ConnectionString
Dim strCom As String = ″ SELECT * FROM student ″
'创建一个 DataSet
myDataSet = New DataSet()
myConn.Open()
'用 OleDbDataAdapter 得到一个数据集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter(strCom, myConn)
'把Dataset绑定student数据表
myCommand.Fill(myDataSet, ″student″)
'关闭此OleDbConnection
myConn.Close()
End Sub
'初始化窗体中的组件
Private Sub InitializeComponent()
……(省略部分自动生成代码)
End Sub
'将各个文本框控件绑定到数据库的各个字段。
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TxtStuID.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuID″))
TxtStuName.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuName″))
TxtStuSex.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuSex″))
TxtStuBorn.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuBorn″))
TxtStuCore.DataBindings.Add(New Binding(″Text″, Me.myDataSet, ″student.StuCore″))
End Sub
End Class
Module Module1
Sub Main()
Application.Run(New Form1())
End SubEnd Module