Aracılığıyla paylaş


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:

Creating the Project

To create the new project

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

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

  1. On the Data menu, click Show Data Sources.

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

  3. Click Next on the Data Source Configuration Wizard welcome page.

  4. Select Database on the Choose a Data Source Type page.

  5. On the Choose your Data Connection page, do one of the following:

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

  7. Click Next on the Save connection string to the Application Configuration file page.

  8. Expand the Tables node on the Choose your Database Objects page.

  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.

To create a connection to the Access version of Northwind

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

  2. 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).

  3. Click OK.

  4. Click Next on the Save connection string to the Application Configuration file page.

  5. Expand the Tables node on the Choose your Database Objects page.

  6. Select the Customers and Orders tables, and then click Finish.

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

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

Editing Data in Your Application

Validating Data

Saving Data

Data Walkthroughs