Using Visual Studio 2010 with a C# WPF project. I know how to create an Access 2000-2003 db file using the ADOX namespace.
ADOX.CatalogClass cat = new CatalogClass();
string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source=" + _dbPath + ";";
cat.Create(str);
cat = null;
Connect and open the database:
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ _dbPath + ";");
//connect to it with a password
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _dbPath + ";User Id=admin;Password=myPassword;"
I want to create the file password protected:
ADOX.CatalogClass cat = new CatalogClass();
string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source="
+ _dbPath + ";Password=myPassword;";
cat.Create(str);
cat = null;
This produces an error at runtime:
Cannot start your application. The workgroup 开发者_运维知识库information file is missing or opened exclusively by another user.
When I create without the password, the database is created successfully.
How can I create a password protected Access database with this strategy?
If you mean a database password, rather than user level security, which requires a workgroup file, you can change the connection string to include:
Jet OLEDB:Database Password=MyDbPassword;
That is:
ADOX.Catalog cat = new CatalogClass();
string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source="
+ _dbPath + ";Jet OLEDB:Database Password=MyDbPassword;";
cat.Create(str);
cat = null;
-- http://www.connectionstrings.com/access
Using the information here: http://support.microsoft.com/kb/317881, with some minor changes:
ADOX._Catalog cat = new ADOX.Catalog();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=PW;" +
"Data Source=C:\\Docs\\NewMDB.mdb;");
I find I can create a password protected Access database.
Not ADOX (64bit issue) ... but you may find useful this slightly different approach.
...
...
using Access = Microsoft.Office.Interop.Access;
using System.Data.OleDb;
...
...
string AccessDB = "C:\users\you\desktop\yourdb.accdb";
oAccess = new Access.Application();
oAccess.NewCurrentDatabase(AccessDB);
oAccess.Quit();
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+AccessDB+".accdb;Mode=12");
con.Open();
// NOTE THE "", '', `` -- that matters
// NOTE NEW PWD is first THE OLD PWD last
new OleDbCommand(@"ALTER DATABASE PASSWORD `XXxx11` ``", con).ExecuteNonQuery();
con.Close();
con.Dispose();
...
...
// OPEN Later ... and do something
con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AccessDB + ".accdb;Mode=12;Jet OLEDB:Database Password=XXxx11");
con.Open();
new OleDbCommand(@"CREATE TABLE XYZ ('ID' Number)", con).ExecuteNonQuery();
con.Close();
con.Dispose();
精彩评论