Walkthrough: Connecting to Data in a SQL Server Express Database
Connecting your application to data in SQL Server Express databases is simplified by using the Data Source Configuration Wizard. After you complete the wizard, data is available in the Data Sources Window for dragging onto forms.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
Tasks illustrated in this walkthrough include:
Creating a new Windows Application project.
Creating and configuring a dataset in your application based on a SQL Server Express database using the Data Source Configuration Wizard.
Prerequisites
In order to complete this walkthrough, you need:
- Access to the SampleDatabase.mdf database created by completing the following walkthrough; Walkthrough: Creating a SQL Server Express Database.
Creating the Project
To create the new project
From the File menu, create a new project.
Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.
The project is created and added to Solution Explorer.
Starting the Wizard
The dataset is created by running the Data Source Configuration Wizard.
Select a SQL Express database when creating your connection. For more information, see How to: Connect to Data in a SQL Server Express Database.
To create a connection to the SQL Express database
On the Data menu, click Show Data Sources.
In the Data Sources window, choose Add New Data Source.
Select Database on the Choose a Data Source Type page, and then click Next.
Select New Connection to create a new data connection.
Note
If the Data source is not Microsoft SQL Server Database File, then select Change to open the Choose/Change Data Source dialog box.
In the Choose Data Source dialog box, select Microsoft SQL Server Database File. Click Continue.
The default provider is .NET Framework Provider for SQL Server. For more information, see Choose/Change Data Source Dialog Box.
Enter the path to the database file you want to access, or click the Browse button to locate the database file.
Note
For this walkthrough select the SampleDatabase.mdf file created by completing the procedures in Walkthrough: Creating a SQL Server Express Database.
Select the authentication mode and login information required by your database. For more information, see Add/Modify Connection (Microsoft SQL Server Database File).
Click OK, and then click Next.
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 choose 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.
Click Yes to copy the database file to your project.
Click Next on the Save connection string to the Application Configuration file page.
Expand the Tables node, select Customers and Orders, and then click Finish.
The SampleDatabaseDataSet is added to your project and the Customers and Orders tables appear in the Data Sources window.
Adding Controls to the Form
Create the data-bound controls by dragging items from the Data Sources window onto Form1.
To create data-bound controls on the form
Drag 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.
Running the Application
To run the application
- Press F5 to run the application.
Next Steps
The dataset you just created is now available in the Data Sources window.
To add functionality to your application
Open the data source in the Dataset Designer to add or edit the objects that make up the dataset.
Add validation logic to the ColumnChanging or RowChanging events of the data tables in the dataset. For more information, see Data Validation Overview.
See Also
Tasks
How to: Manage Local Data Files in Your Project
Concepts
Other Resources
Connecting to Data in Visual Studio
Preparing Your Application to Receive Data
Fetching Data into Your Application
Displaying Data on Forms in Windows Applications