当我尝试使用VB.NET连接到Microsoft Access数据库时出现错误。我在网上看到了一些例子。我的代码看起来像这些示例,但是我收到一条构建错误消息,说明:
未定义类型'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
答案 0 :(得分:1)
imports System.Data.OleDb
? 如果是的话,它会给你一个错误吗?
答案 1 :(得分:0)
请尝试修改此行:
Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery, connection)
只放这些:
Dim queryCommand As New System.Data.OleDb.OleDbCommand(readQuery)
queryCommand.Connection = connection
答案 2 :(得分:0)
导入System.Data Imports System.Data.OleDb 模块模块1 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
结束模块