简体   繁体   English

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

[英]How can I create user name and password protected MS Access 2007 file from C# and SQL?

I don't know much about OleDB and I need some information on how to create a MS Access 2007 file as password protected. 我不太了解OleDB,我需要一些有关如何创建MS Access 2007文件作为密码保护的信息。 This is a piece of code which use User Id=admin; Password= 这是一段使用User Id=admin; Password=的代码User Id=admin; Password= 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. User Id=admin; Password=但它在尝试保存说法时给出了一个错误: Cannot start your application. The workgroup information file is missing or opened exclusively by another user. 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 编辑:现在我有错误: Cannot open the MS Office Access database engine workgroup information file

I have figured out that problems lay in the SQL command. 我已经发现SQL命令存在问题。 What SQL command should I be using? 我应该使用什么SQL命令? 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: EDITTED改变了我现在拥有的代码:

    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. 问候。

Open connection to DB in exclusive mode , described in Working with Database Passwords in VBA Code . 独占模式打开与DB的连接,如在VBA代码中使用数据库密码中所述

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. ". 以上是“ 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"). 如果仍然Cannot open the MS Office Access database engine workgroup information file错误,请尝试将Jet OLEDB:System database添加到指向System.MDW文件的连接字符串(使用“搜索”找到它)。 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;");

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 . 考虑一下,SQL UPDATE类似于DELETEINSERT结合。 Likewise, combine CREATE and DROP eg 同样,结合CREATEDROP例如

instead of (pesudocode) 而不是(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 ;) 然后GRANT新用户相同的权限老;)

ps I don't think the user name and password values can be parameterized. ps我不认为可以参数化用户名和密码值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM