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.
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
Create a Windows Forms project.
If the Data Sources window isn't showing, choose View, Other Windows, Show Data Sources on the menu bar.
In the Data Sources window, choose the Add New Data Source link.
The Data Source Configuration Wizard appears.
On the Choose a Data Source Type page, choose the Database icon, and then choose the Next button.
On the Choose a Database Model page, choose the Dataset icon, and then choose the Next button.
On the Choose Your Data Connection page, choose the New Connection button.
The Add Connection dialog box appears.
In the Data Source list, verify that Microsoft SQL Server Database File appears. If it doesn't, perform the following steps:
Choose the Change button.
The Change Data Source dialog box appears.
In the Data Source list, choose Microsoft SQL Server Database File, and then choose the OK button.
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.
Under Log on to the server, choose the option button for the authentication mode and login information that your database requires.
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 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.