如何从C#和SQL创建受用户名和密码保护的MS Access 2007文件? [英] How can I create user name and password protected MS Access 2007 file from C# and SQL?
问题描述
我对OleDB不太了解,我需要一些有关如何创建受密码保护的MS Access 2007文件的信息.这是一段使用User Id=admin; Password=
的代码,但是在尝试保存以下内容时却给了我一个错误:Cannot start your application. The workgroup information file is missing or opened exclusively by another user.
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.
现在出现错误:Cannot open the MS Office Access database engine workgroup information file
我发现问题出在SQL命令中.我应该使用什么SQL命令?此命令产生了一个问题,我不知道为什么.我在评论中提供的链接中使用了类似的语法.
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更改了我现在的代码:
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.
推荐答案
OleDbConnection objOleDbConnection = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + sfdNewFile.FileName + ";Exclusive=1;");
这也应该工作正常:
OleDbConnection objOleDbConnection = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + sfdNewFile.FileName + ";Mode=12;");
以上是针对"Cannot change password on a shared open database.
"的.
如果仍然出现Cannot open the MS Office Access database engine workgroup information file
错误,请尝试在连接字符串中添加 Jet OLEDB:System database
指向System.MDW
文件(使用搜索"找到它).可能看起来像:
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;");
这篇关于如何从C#和SQL创建受用户名和密码保护的MS Access 2007文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!