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.
Note
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.
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.
Prerequisites
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.
Creating the Project
To create the new project
From the File menu, create a new project.
Select Windows Forms Application and click OK. For more information, see Developing Client Applications with the .NET Framework.
The project is created and added to Solution Explorer.
Connecting to the Northwind Database
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 menu bar, choose View, Other Windows, 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.
-or-
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.
Adding Controls to the Form
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.
Running the Application
To run the application
Press F5.
Form1 opens, showing the data from the Customers table.
Security
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.
Next Steps
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.
Add validation logic to the ColumnChanging or RowChanging event of the data tables in the dataset. For more information, see Validating Data in Datasets.
See Also
Concepts
Preparing Your Application to Receive Data
Fetching Data into Your Application
Binding Controls to Data in Visual Studio
Editing Data in Your Application