Walkthrough: Connecting to Data in an Access Database (Windows Forms)
You can connect to an Access database (either a .mdf file or a .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. If you want to understand how the project system in Visual Studio manages these local database files, see How to: Manage Local Data Files in Your Project.
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 with 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, choose Other Windows > Data Sources.
In the Data Sources window, click Add New Data Source.
Choose Database on the Choose a Data Source Type page and then choose Next.
Choose Dataset on the Choose a Database Model page and then choose 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
While 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, choose 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 choose OK.
Note
If the database file has a user name and password, specify them before you choose OK.
Choose Next on the Choose your Data Connection page.
Choose Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
Choose whatever tables or views that you want in your DataSet, and then choose 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, choose Other Windows > Data Sources.
In the Data Sources window, click Add New Data Source.
Choose Database on the Choose a Data Source Type page and then choose Next.
Choose Dataset on the Choose a Database Model page and then choose 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), choose Change to open the Change Data Source dialog box and choose Microsoft Access Database File and then choose OK.
In the Database file name, specify the path and name of the .mdb file to which you want to connect and then choose OK.
Choose Next on the Choose your Data Connection page.
Choose Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
Choose whatever tables or views that you want in your DataSet, and then choose 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 Securing Connection Strings.
Next Steps
The dataset 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 Binding 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 Validating Data in Datasets).
See Also
Concepts
Preparing Your Application to Receive Data
Fetching Data into Your Application
Binding Controls to Data in Visual Studio
Editing Data in Your Application