打开密码保护的.mdb文件

时间:2015-03-10 20:15:34

标签: c# ms-access database-connection

dd.TableDefs上的编译器错误...我该改变什么,这样这段代码才能正常运行?它看起来问题来自我试图使用DAO& Microsoft.Ace.OLEDB,但我不知道如何修复。确切的错误是

Error   27  Use of unassigned local variable 'dd'   


DAO.Database dd;
DAO.TableDef tdf1;
DAO.DBEngine db = new DAO.DBEngine();
string newName = "Just_Auto_Linked_In";
string SQLName = "Just_Auto_Linked_In";
string oledbConnectString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data   Source=C:\\Databases\\Testing.mdb;" +
"Jet OLEDB:Database Password=foxtrotcharliealpha;";

using (OleDbConnection connection = new OleDbConnection(oledbConnectString))
{
connection.Open();
string[] tableNames = new string[1] { "Magnus", };
for (int q = tableNames.GetLowerBound(0); q <= tableNames.GetUpperBound(0); q++)
{
    foreach (DAO.TableDef tabledef in dd.TableDefs)
    {
        string viewName = tableNames[q];
        if (tabledef.Name == viewName) { found = true; }
        try { if (found) { dd.TableDefs.Delete(viewName); } }
        catch { }
    }
}
tdf1 = dd.CreateTableDef(newName);
tdf1.Connect = cs1;
tdf1.SourceTableName = SQLName;
dd.TableDefs.Append(tdf1);
}

编辑#1 这将打开一个没有密码的数据库,但如果我无法将密码传递给DAO打开。

DAO.Database dd;
DAO.DBEngine db = new DAO.DBEngine();
string TestDB = "C:\\Databases\\Testing.mdb;" ";
dd = db.OpenDatabase(TestDB);

1 个答案:

答案 0 :(得分:0)

这是VBA,但您应该能够将它移植到C#相对容易。

Sub OpenPwdProtectedDB()
Dim db As DAO.Database
Dim ws As DAO.Workspace
Set ws = DBEngine.WorkSpaces(0)
Set db = ws.OpenDatabase("H:\Database\database.mdb", False, False, "MS Access;PWD=password")
'Do what you want to here
'
'
'
'
db.Close
End Sub