Export (0) Print
Expand All

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.

NoteNote

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.

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:

To create the new project

  1. From the File menu, create a new project.

  2. Select Windows Forms Application and click OK. For more information, see Developing Client 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

  1. On the menu bar, choose View, Other Windows, Data Sources.

  2. In the Data Sources window, select Add New Data Source.

  3. On the Choose a Data Source Type page, select Database, and then click Next.

  4. On the Choose a Database Model page, select Dataset and then click Next.

  5. 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.

  6. If your database requires a password, select the option to include sensitive data, and then click Next.

  7. On the Save connection string to the Application Configuration file page, click Next.

  8. On the Choose your Database Objects page, expand the Tables node.

  9. 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

To run the application

  • Press F5.

    Form1 opens, showing the data from the Customers table.

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.

The dataset you just created is now available in the Data Sources window.

To add functionality to your application

Show:
© 2014 Microsoft