Export (0) Print
Expand All
5 out of 13 rated this helpful - Rate this topic

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. You create a dataset by using the Data Source Configuration Wizard, and you can add data-bound controls by moving them from the Data Sources Window.

When you create a database in Visual Studio, 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.

NoteNote

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.

This walkthrough includes the following tasks:

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.

To create and configure a dataset

  1. Create a Windows Forms project.

    See Developing Client Applications.

  2. If the Data Sources window isn't showing, choose View, Other Windows, Show Data Sources on the menu bar.

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

    The Data Source Configuration Wizard appears.

  4. On the Choose a Data Source Type page, choose the Database icon, and then choose the Next button.

  5. On the Choose a Database Model page, choose the Dataset icon, and then choose the Next button.

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

    The Add Connection dialog box appears.

  7. In the Data Source list, verify that Microsoft SQL Server Database File appears. If it doesn't, perform the following steps:

    1. Choose the Change button.

      The Change Data Source dialog box appears.

    2. In the Data Source list, choose Microsoft SQL Server Database File, and then choose the OK button.

  8. 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 locate that file.

  9. Under Log on to the server, choose the option button for the authentication mode and login information that your database requires.

  10. Choose the OK button, and then choose the Next button.

    NoteNote

    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.

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

  12. On the Save connection string to the application configuration file page, choose the Next button.

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

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.

You can create or modify objects in the dataset if you open the datasource 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.