Access Database password check

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,<br/>
I am trying to set up a log in window before my program connects to an access database and puts the table data in a DataGridView.
The access database (.accdb) has a password, for now it is set to Password01. But inside the program (once logged in and viewing data) you can change the password of the database if you want.
I got the window working with the DataGridView, it shows whats in the database.
My problem is with the Log In. How can I check to see if the entered password matches the password in the access database before continuing?
Here is the code I have populating the DataGridView on the other window:
<pre class="prettyprint OleDbConnection connect = new OleDbConnection();

connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data.accdb;Jet OLEDB:Database Password=Password01;";
connect.Open();

DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("Select * from Table1", connect);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
connect.Close();[/code]
Once I get the log in window working Ill have it pass the correct password over to this window (the code above) if it is correct.<br/>

Here is the code I attempted already that does not work, becuase I always get the message box Yay even if I type a bad password:<br/>

<pre class="prettyprint private void btn_Log_Click(object sender, EventArgs e)
{
sPassword = txt_password.Text;
bool correct = false;

try
{
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data.accdb;Jet OLEDB:Database Password=" + sPassword + ";";
correct = true;
}
catch
{
MessageBox.Show(this, "ERROR:nEither the file path or password is incorrect, try again.", "ERROR");
correct = false;
}
if (correct)
{
MessageBox.Show("Yay");
}

}[/code]
<br/>
<span style="text-decoration:underline So in my log in window, how do I check once the Log In button is pressed if the password entered matches that of the password in the access database? If not, then it wont move on, but I can handle that part.
The password wont always be the same if it is changed later on, so I cant just have Password01 hardcoded into the connection string...
<span style="text-decoration:underline Thank You!

View the full article
 
Back
Top