Bagikan melalui


How to: Connect to Data in an Access Database

You can establish communication between your application and an Access database by creating a connection that points to the actual database file (.mdb).

You connect to data in Access files by running the Data Source Configuration Wizard and selecting Database on the Choose a Data Source Type page.

Tip

Drag an .mdf or .mdb file from Windows Explorer into Solution Explorer to automatically configure your connection and start the Data Source Configuration Wizard ready for you to select the objects to use in your application.

You can start the wizard by selecting the Add New Data Source command from the Data menu or from within the Data Sources window. Alternatively, you can select the Add Existing Item command from the Project menu and browse to the desired database file.

After completing the wizard, a copy of the database (.mdb file) and a strongly typed dataset file (.xsd) are added to your project. The selected database objects are immediately available in the Data Sources Window for dragging onto your form. For more information, see Displaying Data Overview.

Note

When adding the .mdb file through the Data Source Configuration Wizard, you are given the choice of adding the file to your project or leaving it in its original location. For more information, see How to: Manage Local Data Files in Your Project.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To connect to data in an Access database with the Add New Data Source command

  1. On the Data menu, click Add New Data Source.

  2. Select Database on the Choose a Data Source Type page.

  3. Select New Connection to create a new data connection.

  4. In the Choose Data Source dialog box, click Microsoft Access Database File in the Data source box.

    The default provider is .NET Framework Provider for OLE DB. For more information, see Choose/Change Data Source Dialog Box.

  5. In the Database file name box, enter the path to the .mdb file, or click the Browse button to locate the database file.

  6. Enter login information if required by your database. For more information, see Add/Modify Connection (Microsoft Access).

  7. Click OK, and then click Next.

  8. Select Yes to copy the .mdb file into your project, or No to connect to the database file in its current location. For more information, see How to: Manage Local Data Files in Your Project.

  9. Click Next.

  10. Expand the tree of objects, and select the database objects to use in your application.

  11. Replace the default DataSet name if desired.

  12. Click Finish.

    The dataset you just created is now available in the Data Sources window. Open the Data Sources window by selecting Show Data Sources from the Data menu.

To connect to data in an Access database with the Add Existing Item command

  1. Select Add Existing Item from the Project menu, or drag the .mdb file from Windows onto Solution Explorer.

  2. Expand the tree of objects, and select the database objects to use in your application.

  3. Replace the default DataSet name if desired.

  4. Click Finish.

    The dataset you just created is now available in the Data Sources window. Open the Data Sources window by selecting Show Data Sources from the Data menu.

Next Steps

To add functionality to your application

  • Select items in the Data Sources window and drag them onto a form. For more information, see Displaying Data Overview.

    Note

    If the data source does not appear in the Data Sources window, click the Refresh button in the window.

Security

Storing sensitive information (such as the server name, user name, and 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 (ADO.NET).

See Also

Tasks

How to: Manage Local Data Files in Your Project

Walkthrough: Displaying Data on a Form in a Windows Application

How to: Connect to Data in a Database

How to: Connect to Data in a Web Service

How to: Connect to Data in an Object

How to: Connect to Data in a SQL Server Express Database

Concepts

Local Data Overview

TableAdapter Overview

Dataset Designer

Data Sources Overview

Change History

Date

History

Reason

Corrected a bug in the “To connect to data in an Access database with the Add New Data Source command” procedure.

Customer feedback.