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.
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:
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
Create a Windows Forms project, and name it ConnectLocalData.
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.
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.
On the Choose Your Data Connection page, choose the New Connection button.
The Choose Data Source dialog box appears.
In the Data source list, choose Microsoft SQL Server Database File, and then choose the Continue button.
The Add Connection dialog box appears.
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.
Under Log on to the server, accept the default values, choose the OK button, and then choose the Next button.
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.
In the dialog box that appears, choose Yes to copy the database file to your project.
On the Save the Connection String to the Application Configuration File page, choose the Next button.
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
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.
To run the application and show the data that you added in the previous walkthrough, choose the F5 key.
Choose the yellow addition icon (), add a customer record, and then save your changes by choosing the disk icon ().
Delete the record that you just created by choosing it and then choosing the red delete icon ().