views:

41

answers:

1

Hey,

I would like to load some data from an access-database in my C#-application. But when it tries to make a connection with the database, I get this error:

Unrecognised database format

How do I solve this?

This is my code:

private void btnBrowseDB_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "Kies een databank.";
    ofd.Filter = "Microsoft Access Databank (*.accdb)|*.accdb";
    ofd.RestoreDirectory = true;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        txtDBLocation.Text = ofd.FileName;
        loadCampagnes(ofd.FileName);
    }
}

private void loadCampagnes(string pathDB)
{
    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
                        + "User ID=;"
                        + "Password=;"
                        + "Data Source =" + pathDB + ";";
    try
    {
        OleDbConnection oleDbConn = new OleDbConnection(connString);

        string strSQL = "SELECT id, naam "
                        + "FROM Marketingcampagne ";

        OleDbCommand oleDbCmd = new OleDbCommand(strSQL, oleDbConn);
        oleDbConn.Open();
        OleDbDataReader oleDbReader = oleDbCmd.ExecuteReader();

        while (oleDbReader.Read())
        {
            string strCampagneNaam = "";
            int intValue;

            int intField = oleDbReader.GetOrdinal("naam");
            if (!oleDbReader.IsDBNull(intField))
                strCampagneNaam = oleDbReader.GetString(intField);

            intField = oleDbReader.GetOrdinal("id");
            if (!oleDbReader.IsDBNull(intField))
                intValue = oleDbReader.GetInt32(intField);

            lbCampagnes.Items.Add(strCampagneNaam);
        }

        oleDbConn.Close();
    }

    catch (OleDbException ex)
    {
        MessageBox.Show("Problemen bij het ophalen van de data (" + ex.Message + ")");
    }

    catch (Exception ex)
    {
        MessageBox.Show("Onbekende fout (" + ex.Message + ")");
    }
}

Vincent

ps. I use Windows 7 64-bit, but I have changed the target CPU to x86.

A: 

For accdb, you need:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;

-- http://www.connectionstrings.com/access-2007

Remou