Imports System.Data.OleDb     '引入命名空间

Public Class 例12_7
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source =f:\stu_manager.mdb"  ' 定义连接字符串
        Dim Conn As New OleDbConnection(str)  ' 创建OleDbConnection对象
        Dim fldNames, stuRec As String
        Conn.Open()     ' 打开数据库连接
        '建立Command对象，并发送从stuinfo表中读取所有记录的SQL命令
        Dim command As New OleDbCommand("Select* From stuinfo", Conn)
        Dim dr As OleDbDataReader   ' 创建OleDbDataReader对象
        dr = command.ExecuteReader  ' 执行命令
        TextBox1.Text = ""   ' 清空文本框
        ' 读取记录集中的每个字段名，并显示在文本框中
        fldNames = " "
        For i = 0 To dr.FieldCount - 1
            fldNames = fldNames & dr.GetName(i) & "       "
        Next
        TextBox1.Text = TextBox1.Text & fldNames & vbCrLf
        Do While dr.Read()  ' 使dr对象前进到下一条记录
            stuRec = ""
            ' 读取每一条记录中各个字段的数据
            For i = 0 To dr.FieldCount - 1
                stuRec = stuRec & dr.Item(i).ToString & "   "
            Next
            ' 将当前记录添加到文本框中
            TextBox1.Text = TextBox1.Text & stuRec & vbCrLf
        Loop
        dr.Close()      ' 关闭OleDbDataReader对象
        Conn.Close()     '关闭数据库连接

    End Sub
End Class