问题
I don't know much about OleDB and I need some information on how to create a MS Access 2007 file as password protected. This is a piece of code which use User Id=admin; Password=
but it gives me an error while trying to save saying: Cannot start your application. The workgroup information file is missing or opened exclusively by another user.
EDIT:
Now I have error: Cannot open the MS Office Access database engine workgroup information file
I have figured out that problems lay in the SQL command. What SQL command should I be using? This command creates a problem, and I can't figure out why. I have used similar syntax from the link that person provided in the comment.
try
{
objOleDbConnection.Open();
objOleDbCommand.CommandText =
"ALTER USER " + storedAuth.UserName +
" PASSWORD [" + storedAuth.Password + "] []";
objOleDbCommand.ExecuteNonQuery();
}
I could use following code, but what about user name?
objOleDbCommand.CommandText = "ALTER DATABASE PASSWORD " + storedAuth.Password + "[]";
EDITTED changed the code what I have now:
private void sfdNewFile_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
// Creating a ADOX object needed to create
// new MS Access file.
ADOX.Catalog createMSFile = new ADOX.Catalog();
createMSFile.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
sfdNewFile.FileName);
Table nTable = new Table();
nTable.Name = "PersonData";
nTable.Columns.Append("DataID", DataTypeEnum.adInteger, 40);
nTable.Columns.Append("Type", DataTypeEnum.adVarWChar, 40);
nTable.Columns.Append("URL", DataTypeEnum.adVarWChar, 40);
nTable.Columns.Append("SoftwareName", DataTypeEnum.adVarWChar, 40);
nTable.Columns.Append("SerialCode", DataTypeEnum.adVarWChar, 40);
nTable.Columns.Append("UserName", DataTypeEnum.adVarWChar, 40);
nTable.Columns.Append("Password", DataTypeEnum.adVarWChar, 40);
createMSFile.Tables.Append(nTable);
// It is importnat to release COM object, in this very order
// otherwise we eill end up with an error.
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(nTable);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(createMSFile.Tables);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(createMSFile.ActiveConnection);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(createMSFile);
OleDbConnection objOleDbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + sfdNewFile.FileName);
OleDbCommand objOleDbCommand = objOleDbConnection.CreateCommand();
try
{
objOleDbConnection.Open();
objOleDbCommand.CommandText = "ALTER DATABASE PASSWORD [" + storedAuth.Password + "] []";
objOleDbCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
// Displaying any errors that
// might have occured.
MessageBox.Show("Error opening the " +
"connection: " + ex.Message);
}
finally
{
objOleDbConnection.Close();
}
MessageBox.Show("File have been created.");
}
Hope for some tips. Regards.
回答1:
Open connection to DB in exclusive mode, described in Working with Database Passwords in VBA Code.
OleDbConnection objOleDbConnection = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + sfdNewFile.FileName + ";Exclusive=1;");
this should work fine as well:
OleDbConnection objOleDbConnection = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + sfdNewFile.FileName + ";Mode=12;");
Edit:
The above is for "Cannot change password on a shared open database.
".
If you still have an Cannot open the MS Office Access database engine workgroup information file
error try add Jet OLEDB:System database to connection string that points to System.MDW
file (locate it using "search"). It might looks like:
OleDbConnection objOleDbConnection = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0" +
";Data Source=" + sfdNewFile.FileName +
";Jet OLEDB:System database=C:\...\System.MDW"
";Exclusive=1;");
回答2:
I don't think you can change a user name directly. Consider than a SQL UPDATE
is analogous to a DELETE
combined with an INSERT
. Likewise, combine CREATE
and DROP
e.g.
instead of (pesudocode)
ALTER USER HelpNeeder SET uid = onedaywhen; -- no such syntax
try (actual code):
CREATE USER onedaywhen pwd H3sJaZ9k2m;
DROP USER HelpNeeder;
Then GRANT
the new user the same privileges as the old ;)
p.s. I don't think the user name and password values can be parameterized.
来源:https://stackoverflow.com/questions/8264602/how-can-i-create-user-name-and-password-protected-ms-access-2007-file-from-c-sha