VB.NET recognizes Access data engine automatically defines data source

VB.NET recognizes the Access data engine and automatically defines the data source
code as follows:

Imports System.Data.OleDb

Public Class MyForm

    Private Dbname As String = "Config.mdb"
    '数据表名:数据, 数据列:ID和数据1
    Dim cn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As DataSet
    Dim cm As OleDbCommand
    
Sub OpenMyDb()
        Dim bit As Integer = System.Runtime.InteropServices.Marshal.SizeOf(IntPtr.Zero) * 8
        Dim str As String
               Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
                Dim subkey As Microsoft.Win32.RegistryKey = key.OpenSubKey("software\ODBC\ODBCINST.INI\Microsoft Access Driver (*.mdb, *.accdb)", True)
        Dim subkeyA As Microsoft.Win32.RegistryKey = key.OpenSubKey("software\ODBC\ODBCINST.INI\Microsoft Access Driver (*.mdb)", True)
        If IsNothing(subkey) Then
            If IsNothing(subkeyA) Then
                '子键不存在
                MsgBox("您的 " & bit & " 位操作系统没有安装 JET 或 ACE 数据库引擎!" & vbCrLf & vbCrLf & "请下载安装 AccessDatabaseEngine 数据库引擎 " & bit & "位版本")
            Else
                MsgBox("您的 " & bit & " 位操作系统安装了 JET 数据库引擎!")
                str = ("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & Dbname)
            End If
        Else
            '有 ACE 数据库引擎
            MsgBox("您的 " & bit & " 位操作系统安装了 ACE 数据库引擎!")
            str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dbname)
        End If
        End Sub
'以下是 Access 数据库 代码

        cn = New OleDbConnection(str)
        Dim sql As String = "SELECT 数据.ID, 数据.数据1 FROM 数据"
        da = New OleDbDataAdapter(sql, cn)
        ds = New DataSet
        da.Fill(ds, "数据") '查询到的数据表保存到ds
        DataGridView1.DataSource = ds.Tables(0) '数据显示到数据表预览控件
End Class

Guess you like

Origin blog.csdn.net/zyyujq/article/details/102932055