1

I am new in creating application in Visual Studio 2010. I recently created an application which has a MySQL as a database. Now, I am creating an app where I used a MS Access 2007 as a database.

I have this code for the database connection of MySQL:

class DBConn
{
    string MyConString = "SERVER=localhost;" + "DATABASE=payroll;" + "UID=root;" + "PASSWORD=admin;";
    public DataTable retrieveRecord(string cmd)
    {
        MySqlConnection con = new MySqlConnection(MyConString);
        MySqlCommand command = new MySqlCommand(cmd, con);
        MySqlDataAdapter adp = new MySqlDataAdapter(command);
        con.Open();
        DataSet set = new DataSet();
        adp.Fill(set);
        con.Close();
        return set.Tables[0];
    }
}

My problem now is that how to change this code to access the database of MS Access 2007? Please help. Thanks.

sean
  • 9,198
  • 22
  • 65
  • 80

1 Answers1

3

Change the connection string to something like this: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\yourdatabase.mdb;User Id=username;Password=password;"

twaggs
  • 3,591
  • 1
  • 14
  • 8
  • if the database is .mdb use this answer but if it is 2007 .accdb use answer in this link http://stackoverflow.com/questions/4427523/how-to-connect-to-access-2007-with-c-sharp – ahmedsafan86 May 25 '12 at 17:54