1 out of 3 rated this helpful - Rate this topic

Walkthrough: Connecting to Data in a SQL Server Express LocalDB Database (Windows Forms)

You can use the Data Source Configuration Wizard to quickly and easily connect your application to data in a SQL Server Express LocalDB database. After you complete the wizard, you can drag data objects in the Data Sources Window onto forms to create data-bound controls.

When creating a new service-based database in Visual Studio 2012, the SQL Server Express LocalDB engine is used to access a SQL Server 2012 database file (.mdf). In earlier versions of Visual Studio, the SQL Server Express engine is used to access a database file (.mdf). For more information, 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 Visual Studio Settings.

This walkthrough includes the following tasks:

In order to complete this walkthrough, you need:

To create the new project

  1. From the File menu, create a new project.

  2. Select Windows Forms Application and choose the OK button. For more information, see Developing Client Applications.

    The project is created and added to Solution Explorer.

The dataset is created by running the Data Source Configuration Wizard.

Select a SQL Server Express LocalDB database when you create your connection. For more information, see How to: Connect to Data in a Database.

To create a connection to the SQL Server Express LocalDB database

  1. On the Data menu, choose Show Data Sources.

  2. In the Data Sources window, choose Add New Data Source.

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

  4. On the Choose a Database Model page, select Dataset, and then choose the Next button.

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

    Note Note

    If the Data source is not Microsoft SQL Server Database File, then select Change to open the Choose/Change Data Source dialog box.

  6. In the Choose Data Source dialog box, select Microsoft SQL Server Database File. Choose the Continue button.

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

  7. Type the path of the database file that you want to access, or click the Browse button to locate the database file.

    Note Note

    For this walkthrough, select the SampleDatabase.mdf file created by completing the procedures in Walkthrough: Creating a SQL Server Express LocalDB Database.

  8. Select the authentication mode and login information that is required by your database. For more information, see Add/Modify Connection (Microsoft SQL Server Database File).

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

    Note Note

    Local database files can be included in a project as a file in the solution. When creating connections to local database files, you can decide between creating a copy of the database in your project, or connecting to the existing database file in its current location. For more information, see How to: Manage Local Data Files in Your Project.

  10. Choose Yes to copy the database file to your project.

  11. On the Save connection string to the Application Configuration file page, choose the Next button.

  12. Expand the Tables node, select Customers and Orders, 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.

Create the data-bound controls by dragging items from the Data Sources window onto Form1.

To create data-bound controls on the form

To run the application

  • Press F5 to run the application.

The dataset you just created is now available in the Data Sources window.

To add functionality to your application

  1. Open the data source in the Dataset Designer to add or edit the objects that make up the dataset.

  2. Add validation logic to the ColumnChanging or RowChanging events of the data tables in the dataset. For more information, see Validating Data in Datasets.

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.