Walkthrough: Connecting to Data in a Database (Windows Forms)
You can connect your application to data by using the Data Source Configuration Wizard in Visual Studio. After you complete the wizard, you can drag data objects from the Data Sources Window onto your forms to create data-bound controls.
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.
Tasks illustrated in this walkthrough includes the following:
Creating a new Windows Forms Application project.
Creating and configuring a dataset in your application based on database objects by using the Data Source Configuration Wizard.
In order to complete this walkthrough, you need:
Access to the Northwind sample database for SQL Server or Access. For more information, see How to: Install Sample Databases.
To create the new project
From the File menu, create a new project.
Select Windows Forms Application and click OK. For more information, see Creating Windows-Based Applications.
The project is created and added to Solution Explorer.
You can connect to the SQL Server version of the Northwind database or to the Microsoft Access version of the database.
To create a connection to the SQL Server or Access version of Northwind
On the Data menu, click Show Data Sources.
In the Data Sources window, select Add New Data Source.
On the Choose a Data Source Type page, select Database, and then click Next.
On the Choose a Database Model page, select Dataset and then click Next.
On the Choose your Data Connection page, select one of the following options:
If a data connection to the Northwind sample database is available in the drop-down list, select it.
Select New Connection to configure a new data connection. For more information, see How to: Connect to Data in a Database.
If your database requires a password, select the option to include sensitive data, and then click Next.
On the Save connection string to the Application Configuration file page, click Next.
On the Choose your Database Objects page, expand the Tables node.
Select the Customers and Orders tables, and then click Finish.
The NorthwindDataSet is added to your project and the Customers and Orders tables appear in the Data Sources window.
You can create data-bound controls by dragging items from the Data Sources window onto your form.
To create data-bound controls on the form
Drag the main Customers node from the Data Sources window onto the form.
Data-bound controls with descriptive labels appear on the form, together with a tool strip (BindingNavigator) for navigating records. A NorthwindDataSet, CustomersTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
Storing connection-string details (such as a password) can affect the security of your application. Using Windows Authentication is a more secure way to control access to a database. For more information, see Protecting Connection Information (ADO.NET).
The dataset you just created is now available in the Data Sources window.
To add functionality to your application
Select other items in the Data Sources window and drag them onto a form. For more information, see Binding Windows Forms Controls to Data in Visual Studio.
Open the data source in the Dataset Designer to add or edit the objects that make up the dataset.