Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\itweb\Documents\Visual Studio 2010\WebSites\notitiae\databases\notitiae.accdb"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "SELECT [tblCounties].* FROM [tblCounties]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim DSCounties As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(DSCounties)
确定。所以我使用上面的代码连接到访问2007 DB。现在我必须添加一个指向具有用户级保护的DB的链接表。研究向我展示了如何在连接字符串中添加用户名和密码。 Dim connectionString As String =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \ itweb \ Documents \ Visual Studio 2010 \ WebSites \ notitiae \ databases \ notitiae.accdb; user name = user; password = pwd ;“
我还了解到我需要添加对工作组文件的引用,“V:\ DATABASE \ WORKDIR \ diodb.mdw”。我的问题是,所有在线引用工作组文件的信息都使用ADODB连接,而不是我的OleDb连接和jet 4而不是ACE 12.添加工作组文件的语法是什么?我试图将ADODB对象添加到我的Visual Studio项目,但VS不会添加ActiveX Data Objects 2.5的引用。这就是我使用OleDB的原因。
答案 0 :(得分:0)
以下Excel VBA代码有效,证明ACE.OLEDB
仍然可以打开具有用户级安全性的.mdb
文件。请注意,.mdw
文件的属性仍使用Jet OLEDB:
前缀。
Sub ulsTest()
Dim con As ADODB.Connection, rst As ADODB.Recordset
Set con = New ADODB.Connection
con.Open _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Users\Public\ulsTest\ulsTest.mdb;" & _
"Jet OLEDB:System Database=C:\Users\Public\ulsTest\Security.mdw;" & _
"User ID=Gord;" & _
"Password=obfuscated;"
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM ulsTable", con
Debug.Print rst(0).Value
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
End Sub
答案 1 :(得分:0)
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=C:\Users\itweb\Documents\Visual Studio 2012\Projects\Notitiae_project\Notitiae_project\App_Data\notitiae.accdb;" & "Jet OLEDB:System Database=V:\DATABASE\WORKDIR\diodb.mdw;" & "User ID=ITWeb;" & "Password=Mike1;"
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
Dim queryString As String = "SELECT [tblCounties].* FROM [tblCounties]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim DSCounties As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(DSCounties)