Connect to data in an Access database (Windows Forms)
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
You can connect to an Access database (either an .mdf file or an .accdb file) by using Visual Studio. After you define the connection, the data appears in the Data Sources window. From there, you can drag tables or views onto your forms.
Prerequisites
To use these procedures, you need a Windows Forms application project, and either an Access database (.accdb file) or an Access 2000–2003 database (.mdb file). Follow the procedure that corresponds to your file type.
Creating the dataset for an .accdb file
You can connect to databases created through Access 2013, Office 365, Access 2010, or Access 2007 by using the following procedure.
To create the dataset
Open the Windows Forms application to which you want to connect data.
On the View menu, select Other Windows > Data Sources.
In the Data Sources window, click Add New Data Source.
Select Database on the Choose a Data Source Type page, and then select Next.
Select Dataset on the Choose a Database Model page, and then select Next.
On the Choose your Data Connection page, select New Connection to configure a new data connection.
Change the Data source to .NET Framework Data Provider for OLE DB.
Important
Although a data source of Microsoft Access Database File (OLE DB) might seem like the right choice, you use that data-source type only for .mdb database files.
In OLE DB Provider, select Microsoft Office 12.0 Access Database Engine OLE DB Provider.
In Server or file name, specify the path and name of the .accdb file to which you want to connect, and then select OK.
Note
If the database file has a user name and password, specify them before you select OK.
Select Next on the Choose your Data Connection page.
Select Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
Select whatever tables or views you want in your dataset, and then select Finish.
The dataset is added to your project, and the tables and views appear in the Data Sources window.
Creating the dataset for an .mdb file
You create the dataset by running the Data Source Configuration Wizard.
To create the dataset
Open the Windows Forms application to which you want to connect data.
On the View menu, select Other Windows > Data Sources.
In the Data Sources window, click Add New Data Source.
Select Database on the Choose a Data Source Type page, and then select Next.
Select Dataset on the Choose a Database Model page, and then select Next.
On the Choose your Data Connection page, select New Connection to configure a new data connection.
If the data source is not Microsoft Access Database File (OLE DB), select Change to open the Change Data Source dialog box and select Microsoft Access Database File, and then select OK.
In the Database file name, specify the path and name of the .mdb file to which you want to connect, and then select OK.
Select Next on the Choose your Data Connection page.
Select Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
Select whatever tables or views you want in your dataset, and then select Finish.
The dataset is added to your project, and the tables and views appear in the Data Sources window.
Security
Storing sensitive information (such as a password) can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see Protecting Connection Information.
Next Steps
The dataset that you just created is now available in the Data Sources window. You can now perform any of the following tasks:
Select items in the Data Sources window and drag them onto your form (see Bind Windows Forms controls to data in Visual Studio).
Open the data source in the Dataset Designer to add or edit the objects that make up the dataset.
Add validation logic to the ColumnChanging or RowChanging event of the data tables in the dataset (see Validate data in datasets).
See Also
Preparing Your Application to Receive Data Bind controls to data in Visual Studio Validating Data Data Walkthroughs