Walkthrough: Connecting to Data in a Database
Connecting your application to data in Visual Studio 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 your 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 database objects using the Data Source Configuration Wizard.
Prerequisites
In order to complete this walkthrough, you need:
- Access to the Northwind sample database. 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 Application and click OK. For more information, see Creating Windows-Based Applications.
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 Microsot Access version of the database. Each process is described in turn in the following two procedures.
To create a connection to the SQL Server version of Northwind
On the Data menu, click Show Data Sources.
In the Data Sources window, select Add New Data Source.
Click Next on the Data Source Configuration Wizard welcome page.
Select Database on the Choose a Data Source Type page.
On the Choose your Data Connection page, do one of the following:
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: Create Connections to SQL Server Databases, or How to: Create Connections to Access Databases.
If your database requires a password, select the option to include sensitive data, and then click Next.
Click Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
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.
To create a connection to the Access version of Northwind
In the Choose Data Source dialog box, select Microsoft Access Database File, and click OK.
If the Add Connection dialog box opens, and the Data source is not Microsoft Access Database File, click Change to open the Choose/Change Data Source dialog box. For more information, see Choose/Change Data Source Dialog Box.
Enter the path to the Northwind.mdb database file, or click the Browse button to locate the Northwind.mdb database file. For more information, see Add/Modify Connection (Microsoft Access).
Click OK.
Click Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
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, along 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 (ADO.NET).
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 Displaying Data Overview.
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 Data Validation Overview.
See Also
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