创建受密码保护的访问数据库。 [英] Create a password protected access database.
问题描述
我正在使用MS Visual Studio 2010,而WPF使用C#。
我知道如何使用ADOX创建访问2000-2003 db文件/>
I`m using MS Visual Studio 2010, and WPF with C#.
I know how to create an access 2000-2003 db file using the ADOX
ADOX.CatalogClass cat = new CatalogClass();
string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source=" + _dbPath + ";";
cat.Create(str);
cat = null;
我也知道如何连接和打开数据库
And I also knew how to connect and open the database
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _dbPath + ";");
如果我用密码手动创建文件来保护它,那么我可以用密码连接它
If I manually create the file with a password to protect it, then I can connect to it with the password
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + _dbPath + ";User Id=admin;Password=myPassword;"
现在,我想创建受密码保护的文件...所以我试过这个:
now, I want to create the file password protected ... so I tried this:
ADOX.CatalogClass cat = new CatalogClass();
string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source=" + _dbPath + ";Password=myPassword;";
cat.Create(str);
cat = null;
但运行我的应用程序时遇到此错误:
无法启动您的应用程序。工作组信息文件丢失或由其他用户独占打开。
请注意,当我创建没有密码时,数据库创建成功。
but I got this error when running my application:
Cannot start your application. The workgroup information file is missing or opened exclusively by another user.
note that when I create without the password, the database is created successfully.
推荐答案
查看此主题 [ ^ ],如果它有帮助。
Have a look at this thread[^], if it helps.
我不确定你是如何从头开始做的。我所做的是将空白数据库文件包含为具有特定密码的资源。然后,我只是将该空白数据库写入文件位置并更改数据库密码。我的代码如下:
I'm not sure how you'd do it from scratch. What I do is include a blank database file as a resource with a specific password. Then, I just write that blank database to the file location and alter the database password. My code looks like:
<System.Diagnostics.DebuggerStepThrough()> _
Public Function CreateDatabase(ByVal Path As String) As String
Dim password As String = ""
Dim newPasswordEntry As New PasswordEntry()
newPasswordEntry.ShowDialog()
newPasswordEntry.Focus()
If newPasswordEntry.result = Windows.Forms.DialogResult.OK Then
password = newPasswordEntry.value
Else
Return ""
End If
Try
File.WriteAllBytes(Path, My.Resources.RecoveryPlanDB_BE_Master)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'now change the password
Dim cn As New OleDbConnection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & _
";Jet OLEDB:Database Password=password;" & _
"Mode=Share Deny Read|Share Deny Write;"
cn.Open()
Dim cmd As New OleDbCommand
cmd.Connection = cn
cmd.CommandText = "ALTER DATABASE PASSWORD [" & password & "][password]"
cmd.ExecuteNonQuery()
cn.Close()
cmd.Dispose()
cn.Dispose()
Return password
End Function
当然,这是VB,所以你必须改变一点(尤其是访问资源中的文件),但这就是主意。
Of course, that's VB, so you'd have to change a bit (especially the accessing the file in the resources), but that's the idea.
试试
try
Database Password=
而不是密码=
这篇关于创建受密码保护的访问数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!