Freigeben über


Walkthrough: Connecting to Data in a Local Database File (Windows Forms)

You can quickly and easily display data from a local database file in your application by creating a dataset and then adding data-bound controls to your application. In this walkthrough, you'll display data from the local database file that you created by following the steps in Walkthrough: Creating a Local Database File in Visual Studio. After you create a Windows Forms project, you'll connect to that database and specify that you want data from the Customers table to appear in a data grid on the form for the application.

When you create a database in Visual Studio 2013, the SQL Server Express LocalDB engine is used to access a database file (.mdf) in SQL Server 2012. In earlier versions of Visual Studio, the SQL Server Express engine is used to access a database file (.mdf). See Local Data Overview.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

This walkthrough includes the following tasks:

Prerequisites

To complete this walkthrough, you need access to the SampleDatabase.mdf database that you create by completing Walkthrough: Creating a Local Database File in Visual Studio.

Creating and configuring a dataset

To create and configure a dataset

  1. Create a Windows Forms project, and name it ConnectLocalData.

    See Developing Client Applications with the .NET Framework.

  2. If the Data Sources window isn't showing, choose the Shift-Alt-D keys or, on the menu bar, choose View, Other Windows, Show Data Sources.

  3. In the Data Sources window, choose the Add New Data Source link.

    In the Data Source Configuration Wizard, choose the Next button two times to accept the default settings.

  4. On the Choose Your Data Connection page, choose the New Connection button.

    The Choose Data Source dialog box appears.

  5. In the Data source list, choose Microsoft SQL Server Database File, and then choose the Continue button.

    The Add Connection dialog box appears.

  6. In the Database file name box, specify the file that you created by completing Walkthrough: Creating a Local Database File in Visual Studio, or choose the Browse button and then locate that file.

    By default, the file is in Users\YourAccount\Documents\Visual Studio Version\Projects\SampleDatabaseWalkthrough\SampleDatabaseWalkthrough.

  7. Under Log on to the server, accept the default values, choose the OK button, and then choose the Next button.

    Note

    When you connect to a local database file, you can create a copy of the database in your project, or you can connect to the database file in its current location. See How to: Manage Local Data Files in Your Project.

  8. In the dialog box that appears, choose Yes to copy the database file to your project.

  9. On the Save the Connection String to the Application Configuration File page, choose the Next button.

  10. On the Choose Your Database Objects page, expand the Tables node, select the Customers and Orders check boxes, and then choose the Finish button.

    The SampleDatabaseDataSet is added to your project, and the Customers and Orders tables appear in the Data Sources window.

Adding data-bound controls

To add data-bound controls

  1. Move the main Customers node from the Data Sources window onto Form1.

    A DataGridView and a tool strip (BindingNavigator) for navigating records appear on the form. A SampleDatabaseDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.

  2. To run the application and show the data that you added in the previous walkthrough, choose the F5 key.

  3. Choose the yellow addition icon (Add button in Windows Form), add a customer record, and then save your changes by choosing the disk icon (Save button in Windows Form).

  4. Delete the record that you just created by choosing it and then choosing the red delete icon (Delete button in Windows Form).

Next Steps

You can create or modify objects in the dataset if you open the data source in the Creating and Editing Typed Datasets. You can also add validation logic to the ColumnChanging or RowChanging events of the data tables in the dataset. See Validating Data in Datasets.

See Also

Tasks

How to: Manage Local Data Files in Your Project

Concepts

Local Data Overview

Binding Windows Forms Controls to Data in Visual Studio

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