Connection of Visual Studio to an Access database

Asked

Viewed 565 times

1

I have a string connection to the Access database:

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\lentes.accdb;Persist Security Info=False";

When I run this code always gives error " There is no support for the keyword 'Provider''."

P.S. has nothing after that code yet, that’s all:

 string strcon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\lentes.accdb;Persist Security Info=False";
            SqlConnection con = new SqlConnection(strcon);
            con.Open();

Then I run Try and catch to see the error and this error I said.

1 answer

3


You are trying to create an ACCESS connection using Assembly System.Data.Sqlclient.

For Acces, you must use Assembly System.Data.Oledbclient

Here an example of code:

public void ConnectToAccess()
{


  System.Data.OleDb.OleDbConnection conn = new 
      System.Data.OleDb.OleDbConnection();
    conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
        @"Data source= C:\Documents and Settings\username\" +
        @"My Documents\AccessFile.mdb";
    try
    {
        conn.Open();
    }
        catch (Exception ex)
    {
        MessageBox.Show("Failed to connect to data source");
    }
    finally
    {
        conn.Close();
    }
}
  • And also can not forget to remove the Provider=...; of the Connection string

  • @Marcelouchimura I believe that in this case the Provider is required. connetionstring for Oledbclient requires the Provider. https://docs.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbconnection.connectionstring?view=netframework-4.7.2#System_Data_OleDb_OleDbConnection_ConnectionString

  • Okay, William, it’s because you’re Oledb, you haven’t touched me. Thanks!

  • Do you think you have to install the OLE DB provider from outside or it comes by default? Some 4 years ago, I messed with . accdb and had to install from outside

  • Download the provider OLE DB Microsoft.ACE.OLEDB here: https://www.microsoft.com/en-us/download/details.aspx?id=54920

  • Assembly System.Data.Oledbclient is native to .NET. What you may need to install is the Access driver, that’s the part. https://www.microsoft.com/en-us/download/details.aspx?id=13255

  • Thank you, William!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.