Search code examples
ms-accessdatacontextdataprovider

DataContext with MSAccess as Data Provider


I try use ms access as data provider but it give me exception.

    static void Main(string[] args)
    {
        DataContext dx = new DataContext(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Data\mail.accdb;Persist Security Info=True");
    }

The exception is

System.ArgumentException was unhandled
Message=Keyword not supported: 'provider'.

How could this happen ?

Thanks in advance,

Brian


Solution

  • Okay I Found the solution. It bit tricky

    First create oledbconnection with access data provider

    IDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Data\mail.accdb;Persist Security Info=True);
    

    Last pass it to DataContext

    DataContext dx = new DataContext(connection);