共用方式為


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

  1. Open the Windows Forms Application to which you want to connect data.

  2. On the View menu, choose Other Windows > Data Sources.

    View Other Windows Data Sources

  3. In the Data Sources window, click Add New Data Source.

    Add New Data Source

  4. Choose Database on the Choose a Data Source Type page and then choose Next.

  5. Choose Dataset on the Choose a Database Model page and then choose Next.

  6. On the Choose your Data Connection page, select New Connection to configure a new data connection.

  7. Change the Data source to .NET Framework Data Provider for OLE DB.

    Change Data Provider to 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.

  8. In OLE DB Provider, choose Microsoft Office 12.0 Access Database Engine OLE DB Provider.

    OLE DB Provider Microsoft Office 12.0 Access

  9. 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.

  10. Choose Next on the Choose your Data Connection page.

  11. Choose Next on the Save connection string to the Application Configuration file page.

  12. Expand the Tables node on the Choose your Database Objects page.

  13. 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

  1. Open the Windows Forms Application to which you want to connect data.

  2. On the View menu, choose Other Windows > Data Sources.

    View Other Windows Data Sources

  3. In the Data Sources window, click Add New Data Source.

    Add New Data Source

  4. Choose Database on the Choose a Data Source Type page and then choose Next.

  5. Choose Dataset on the Choose a Database Model page and then choose Next.

  6. On the Choose your Data Connection page, select New Connection to configure a new data connection.

  7. 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.

  8. In the Database file name, specify the path and name of the .mdb file to which you want to connect and then choose OK.

    Add Connection Access Database File

  9. Choose Next on the Choose your Data Connection page.

  10. Choose Next on the Save connection string to the Application Configuration file page.

  11. Expand the Tables node on the Choose your Database Objects page.

  12. 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

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

Validating Data

Saving Data

Other Resources

Connecting to Data in Visual Studio

Data Walkthroughs