VB.NET 连接到 MS Access

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13499418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:15:20  来源:igfitidea点击:

VB.NET connection to MS Access

vb.netms-accessconnection

提问by DMalerman

I get an error when I am trying to connect to a Microsoft Access DB using VB.NET. I see examples all over the web. My code looks like those examples, however I am getting a build error message stating:

当我尝试使用 VB.NET 连接到 Microsoft Access DB 时出现错误。我在网上看到了很多例子。我的代码看起来像那些示例,但是我收到一条构建错误消息,说明:

Type 'System.Data.OleDb.OleDbConnection' is not defined.

未定义类型“System.Data.OleDb.OleDbConnection”。

I have tried adding some kind of import statement for the system.data.oledb... but that does not seem to work. My code is below. It is a basic connection so I am thinking that I am missing some kind of add in, library, or setting. Any and all help would be greatly appreciated.

我曾尝试为system.data.oledb...添加某种导入语句,但这似乎不起作用。我的代码如下。这是一个基本的连接,所以我想我缺少某种插件、库或设置。任何和所有的帮助将不胜感激。

Public Function TestMain(ByVal args() As Object) As Object
    ' Connection String to MS Access DB
    Dim connectStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                             "Data Source=C:\Users\DMalerman\keyword.accdb;" & _
                             "Persist Security Info=False;"
    MsgBox(connectStr)
    ' Create connection to the db
    Using connection As New System.Data.OleDb.OleDbConnection(connectStr)
        ' Create the SQL Query
        Dim readQuery As String = "Select KeywordDriver.ScriptName from KeywordDriver " & _
                                    "where KeywordDriver.Keyword = test"
        Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery, connection)

        'Open the Connection
        connection.Open()

        ' Query the Database
        Dim dbReader As System.Data.OleDb.OleDbDataReader = queryCommand.ExecuteReader()

        ' Loop until there is nothing left to read
        While dbReader.Read()
            Dim sKeyword As String = ""
            sKeyword = dbReader.GetString(0)
            MsgBox(sKeyword)
        End While
        ' Close the Reader
        dbReader.Close()

    End Using

    Return Nothing
End Function

回答by Beth

did you try

你试过了吗

imports System.Data.OleDb

? if so, did it give you an error?

? 如果是这样,它是否给你一个错误?

回答by Alex

Please try to modify this line: Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery, connection)

请尝试修改此行: Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery, connection)

by putting these only: Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery)queryCommand.Connection = connection

只放这些: Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery)queryCommand.Connection = connection

回答by uld

Imports System.Data Imports System.Data.OleDb Module Module1 Public str As String Public con As OleDbConnection Public cmd As OleDbCommand Public dtreader As OleDbDataReader Public dtadapter As OleDbDataAdapter

导入 System.Data 导入 System.Data.OleDb 模块 Module1 Public str As String Public con As OleDbConnection Public cmd As OleDbCommand Public dtreader As OleDbDataReader Public dtadapter As OleDbDataAdapter

Public Sub openconn()
    str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database2.mdb"
    con = New OleDbConnection(str)

    Try
        con.Open()
    Catch ex As Exception
        MessageBox.Show("gagal koneksi")
    End Try
End Sub

End Module

终端模块