Udostępnij za pośrednictwem


How to: Connect to Data in a Database

You connect your application to a database by running the Data Source Configuration Wizard and selecting Database on the Choose a Data Source Type page.

Upon completion of the wizard, a dataset is added to your project and is immediately available in the Data Sources Window for dragging onto your form.

Dragging items from the Data Sources window to a form or component results in the creation of several data-related objects, including the connection object. The connection object is located within the TableAdapter associated with the dragged items. For example, if you drag a column from a table named Customers, a TableAdapter named CustomersTableAdapter is created. To view the connection, open the dataset in the Dataset Designer, select the TableAdapter, and view the Connection property in the Properties window. For more information, see How to: Edit a Connection String.

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.

To connect your application to a database

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

  2. On the Data menu, click Add New Data Source.

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

  4. Select a data connection from the list of available connections on the Choose Your Data Connection page. If your desired data connection is not available, select New Connection to create a new data connection. For more information on creating a new connection to a database, see one of the following Help pages:

  5. By default, the connection is saved in the application configuration file. Clear the check box to save the connection string directly in the compiled application. For more information, see How to: Save a Connection String.

  6. Select the database objects to use in your application.

  7. Replace the default DataSet name, if you want to.

  8. Click Finish.

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

  9. Open the Data Sources window by clicking Show Data Sources on the Data menu.

  10. Drag items from the Data Sources window to a form or the Component Designer.

Security

Storing sensitive information (such as a password) can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see Protecting Connection Information (ADO.NET).

See Also

Concepts

Displaying Data Overview

Other Resources

Data Walkthroughs

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