Wednesday, October 30, 2013

Setting Password to Mdb File through C# code

I want to set Password to MS Access 2010 file through C# code. Can you help me?



try
{
if (File.Exists(FilePath))
{
string OledbConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;User Id=Admin;Password={1};Data Source={0};Mode=12;", FilePath, OldPassword);
using(OleDbConnection con = new OleDbConnection(OledbConnectionString))
{
string sql = string.Empty;
sql = string.Format("ALTER DATABASE PASSWORD [{0}] [{1}]", NewPassword, OldPassword);
OleDbCommand cmd = new OleDbCommand(sql, con);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
return "";
}
else
{
return "File does not exist.";
}
}
catch (Exception ex)
{
return ex.Message;
}

When I run this code for the 1st time,it is getting executed successfully. But for the next time,it is giving me exception that "Cannot start your application. The workgroup information file is missing or opened exclusively by another user.".

No comments:

Post a Comment