Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OLEDB connection to Access Database (accdb)

I want to make a simple application for an exercise, so it could be nice to connect to a simple database like Access (.accdb)

My program looks like this:

using System;
using System.Collections.Generic; 
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;

namespace myProject.Account
{
    public class DbManager
    {
       private OleDbConnection _dbConnection;

       public void OpenDbConnection()
       {
        _dbConnection = new OleDbConnection {ConnectionString = GetConnectionString()};
       }

       private string GetConnectionString()
       {
        return "Provider=Microsoft.ACE.OLEDB.14.0;Data Source=exercise1.accdb";
       }

       public void CloseDbConnection()
       {
        _dbConnection.Close();
       }

       public void GetUser()
       {
        DataSet myDataSet = new DataSet();
        var myAdapptor = new OleDbDataAdapter();
        OleDbCommand command = new OleDbCommand("SELECT * FROM tblUser", _dbConnection);
        myAdapptor.SelectCommand = command;
        myAdapptor.Fill(myDataSet, "tblUser");
       } 

    }
  }

I using Visual Studio 2010. When I test my application by using the built in debug mode "Start without Debugging" (CTRL+F5) I get this error:

The 'Microsoft.ACE.OLEDB.14.0' provider is not registered on the local machine.

I have tried to download and install "Microsoft Access Database Engine 2010 Redistributable" (64 bit) from Microsoft omepage: http://www.microsoft.com/download/en/details.aspx?id=13255

Unfortunately it sah not solved the problem. I still got the error when the myAdapptor.Fill() is executed. What is wrong?

like image 517
Jedi Avatar asked Nov 28 '11 21:11

Jedi


People also ask

How do I install OLE DB provider for Access?

Download Microsoft.ACE.OLEDB.12.0 provider Once you click Download it will prompt you to select the architecture i.e. 32 BIT (x86) or 64 BIT (x64). Once downloaded, just right click the Microsoft Access Database Engine 2010 Redistributable file and click install and restart the machine after installation is completed.

How do I get connection string from Access database?

Now get a connection string, Go to Tools menu and select connect to the database and browse database from the Project Directory. Click on Advanced and copy the highlighted text as your connection string. Now make connection using the following code: OleDbConnection con = new OleDbConnection("Provider=Microsoft.


1 Answers

You need the Access 2007 Runtime.

like image 96
Tom Bass Avatar answered Oct 14 '22 17:10

Tom Bass