0
votes

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?

1
Database passwords are not security, but "security theater" -- they are really useless for any practical purpose and should be avoided. - David-W-Fenton
Theatre can be useful. An Access database password is a nice way to make you think before you tamper. - Fionnuala
I disagree with one exception. If you're using an MDB/ACCDB just to send data to somebody via email, or some other method, a db password is better and stronger than, say, a zip file password. But in a production application with multiple users sharing a back-end data file, it's just bloody stupid to use a database password. - David-W-Fenton

1 Answers

1
votes

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.