小编典典

如何从C#和SQL创建受用户名和密码保护的MS Access 2007文件?

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.

编辑:现在我有错误: Cannot open the MS Office Access database engine workgroup information file

我发现问题出在SQL命令中。我应该使用什么SQL命令?此命令产生了一个问题,我不知道为什么。我从评论者提供的链接中使用了类似的语法。

    try
    {
        objOleDbConnection.Open();
        objOleDbCommand.CommandText = 
            "ALTER USER " + storedAuth.UserName + 
            " PASSWORD [" + storedAuth.Password + "] []";
        objOleDbCommand.ExecuteNonQuery();
    }

我可以使用以下代码,但是用户名呢?

objOleDbCommand.CommandText = "ALTER DATABASE PASSWORD " + storedAuth.Password + "[]";

编辑更改了我现在的代码:

    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.");
    }

希望有一些提示。问候。


阅读 193

收藏
2021-05-16

共1个答案

小编典典

独占模式下打开与DB的连接,如在VBA代码中使用数据库密码中所述

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文件的连接字符串(使用“搜索”定位它)。它可能看起来像:

OleDbConnection objOleDbConnection = new OleDbConnection(
    "Provider=Microsoft.ACE.OLEDB.12.0" +
    ";Data Source=" + sfdNewFile.FileName + 
    ";Jet OLEDB:System database=C:\...\System.MDW"
    ";Exclusive=1;");
2021-05-16