I have the following code to verify if an MSAccess 2003 database is opened in Exclusive Mode by another app (database already has a password):
OleDbConnectionStringBuilder conString = new OleDbConnectionStringBuilder();
conString.Provider = "Microsoft.Jet.OLEDB.4.0";
conString.DataSource = "some path to some mdb file";
// I don't care about the password,
// I just whant to know if it is opened in Exclusive Mode
conString["Jet OLEDB:Database Password"] = String.Empty;
conString["Mode"] = "Share Deny None";
string completeConnStr = conString.ConnectionString;
using (OleDbConnection con = new OleDbConnection(completeConnStr))
{
try
{
con.Open();
con.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
When the database is opened in Exclusive Mode it doesn't care about the password, it throws an OleDbException with the following message: "File is already in use". When the database is not in exclusive mode and receives the wrong password it throws an OleDbException with a message: "It is not a valid password".
How can I identify these two exceptions? password verification is made in another method, so I just want to know if the database is opened in exclusive mode before annoying the user with the "Enter Password please" dialog.