Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

Have a look at this thread[^], if it helps.
 
Share this answer
 
Comments
sikas_Cisco 30-Jul-10 11:52am    
thanks, but this is for the connection, I`m talking about creating the file from scratch!
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:
VB
<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


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.
 
Share this answer
 
Comments
William Winner 30-Jul-10 18:13pm    
I should also mention that if you want to avoid SQL injection attacks, then you should use parameters instead of the straight

cmd.CommandText = "ALTER DATABASE PASSWORD [" & password & "][password]"

For my purposes, no one using the database would even know what a SQL injection attack is, nor would it really matter since it's a blank database to begin with, so there's nothing they can screw up.
try
Database Password=
instead of Password=
 
Share this answer
 
Comments
Richard Deeming 13-Jul-15 10:10am    
Do you really think the OP has been sat waiting for an (incorrect) answer for the last five years?!
Erik Rude 29-Jul-15 9:17am    
I was looking for a solution to a similar question. I found this and the only thing the OP needed to do to make this work (and worked in my case) was to change to Database Password= instead of Password=. So it is not a wrong answer, and I gave the solution so that other people looking at a 5 yO post may get a heads up. Sorry if that made your day worse :) Never mind - it may be wrong I see your point.
phil.o 13-Jul-15 10:10am    
This post is 5 years old...
RedDk 13-Jul-15 13:07pm    
Psalms 107:24

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900