Imports System.Data.OleDb
'注意:以上一句必须写,不然就不能用OleDb类。
Public Class Form1
Inherits System.Windows.Forms.Form
'第一步,定义赋值连接数据库的连接字符串
Dim ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= stu_data.mdb"
'第二步,定义和建立连接数据库的连接字符串
Dim conn As OleDbConnection = New OleDbConnection(ConnStr)
'第三步,定义数据库适配器,用来管理数据库中的表
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter
'第四步,定义和建立数据集
Dim ds As DataSet = New DataSet
'注:以上第一、二步可以合成一步。第四步也可以在具体子程序中定义,甚至多个。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'准备查询语句
Dim QueryStr As String = "SELECT * FROM students"
'在adapter管理下,执行conn连结下的查询语句
adapter.SelectCommand = New OleDbCommand(QueryStr, conn)
'将执行结果填充到数据集ds中
adapter.Fill(ds, "students")
'把数据集邦定到Datagrid1表格控件上
DataGrid1.DataSource = ds.Tables(0)
'下面的两个表与前面第一个表的操作类似。
Dim QueryStr1 As String = "SELECT * FROM student_info"
adapter.SelectCommand = New OleDbCommand(QueryStr1, conn)
adapter.Fill(ds, "student_info")
DataGrid2.DataSource = ds.Tables(1)
Dim QueryStr2 As String = "SELECT * FROM prac_desi"
adapter.SelectCommand = New OleDbCommand(QueryStr2, conn)
adapter.Fill(ds, "prac_desi")
DataGrid3.DataSource = ds.Tables(2)