vb连接access数据库

2021-04-07 15:21:19 浏览数 (1)

vb连接access数据库

1插入相应的包

代码语言:javascript复制
Imports System.Data.OleDb

2声明连接参数

代码语言:javascript复制
    Dim OleDbConnection1 As OleDb.OleDbConnection
    Dim strConnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="   Application.StartupPath   "db1.mdb"

3建立连接

代码语言:javascript复制
 OleDbConnection1 = New System.Data.OleDb.OleDbConnection(strConnect)
        OleDbConnection1.Open()

4执行语句查询

代码语言:javascript复制
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader

        cmd.Connection = OleDbConnection1
        '初始化OLEDB命令的语句 就是查询 什么字段从什么表 条件是ID等于你在t1中输入的内容
        cmd.CommandText = "select * from student"
        '执行OLEDB命令以ExecuteReader()方式,并返回一个OLEDBReader,赋值给dr
        dr = cmd.ExecuteReader()
        '判断下dr中是否有数据。如果有就把第一个值赋值给t2的值
        If dr.Read() Then
            TextBox1.Text = dr(1)
        End If
    End Sub

0 人点赞