如何连接到受密码保护的Access数据库

时间:2014-07-08 14:11:37

标签: vb.net

我尝试将我的应用程序连接到受密码保护的数据库,但我发现很难这样做..我使用了这段代码而没有使用密码而且运行没有问题......

    Dim Con As OleDb.OleDbConnection = New OleDb.OleDbConnection
    Dim DBProvider As String
    Dim DBSource As String
    Dim DS As New DataSet
    Dim DA As OleDb.OleDbDataAdapter
    Dim Sqlstr As String

    Try
        DBProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
        DBSource = "Data Source =" & App_Path & "/DBase/Dbx.accdb;"
        Con.ConnectionString = DBProvider & DBSource
        Con.Open()
        Sqlstr = "SELECT * FROM Dat_Barang"
        DA = New OleDb.OleDbDataAdapter(Sqlstr, Con)
        DA.Fill(DS, "Query")

    Catch ex As Exception
        MsgBox(ex.Message)
        Con.Close()
    End Try

然后我尝试使用密码保护我的数据库,并添加了我在互联网上找到的代码,结果与没有密码的结果差别很大..这段代码..

DBSource = "Data Source =" & App_Path & "/DBase/Dbx.accdb;Jet OLEDB:Database Password=1234567890;"

但是当我运行应用程序时,我的应用程序变得错误..

  

错误消息:

     

“无法打开数据库”,它可能不是您的数据库   应用程序识别,或文件可能已损坏。“

我的问题是如何解决这个问题。??感谢。

编辑:

我使用Office 2013 ..

2 个答案:

答案 0 :(得分:0)

Access内,运行修复工具。它位于

Tools > Database Utilities > Compact and repair database

答案 1 :(得分:0)

我多年没有使用Access了,但这是我以前使用的库代码。仔细看看。我最后一次更新它的是.accdb文件格式。

Public Overloads Sub Connect(pDatabase As String, pPassword As String)

    '[set up some paths and check the file exists stuff...]

    Dim vConnection = String.Empty
    If pDatabase.EndsWith(".accdb") Then
        vConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"
    Else
        vConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
    End If
    vConnection &= "Data Source=" & pDatabase & ";"
    vConnection &= "Password='';"               ' Leave blank!
    vConnection &= "User ID=Admin;"             ' You may need to modify this!
    vConnection &= "Mode=16;"                   ' Standard sharing - for network access = adModeShareDenyNone
    vConnection &= "Jet OLEDB:Database Password='" & pPassword & "' ;"  ' Password goes here, not above.
    vConnection &= "Jet OLEDB:Engine Type=5;"
    vConnection &= "Jet OLEDB:Database Locking Mode=0;"         ' Don' t lock 
    vConnection &= "Jet OLEDB:Global Partial Bulk Ops= 2;"
    vConnection &= "Jet OLEDB:Global Bulk Transactions=1;"
    vConnection &= "Jet OLEDB:Create System Database=False;"        ' Do NOT create a system database (.mdw)
    vConnection &= "Jet OLEDB:Encrypt Database=False;"
    vConnection &= "Jet OLEDB:Don't Copy Locale on Compact=False;"
    vConnection &= "Jet OLEDB:Compact Without Replica Repair=False;"
    vConnection &= "Jet OLEDB:SFP=False;"

    ' [do stuff with connections, etc.
End Sub

我希望它对某人有用 - 天知道我可能永远不会再看它了!