2

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

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

我尝试为 ... 添加某种导入语句,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
4

3 回答 3

1

你试过了吗

imports System.Data.OleDb

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

于 2012-11-21T18:12:37.100 回答
0

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

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

于 2012-11-21T19:14:32.660 回答
0

导入 System.Data 导入 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

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

端模块

于 2015-08-19T15:36:37.897 回答