다음을 통해 공유


Connecting to Data in Windows Forms Applications

Visual Studio provides tools to connect your application to data from many different sources, such as databases, web services, and objects. If you are using data design tools in Visual Studio, you often do not need to explicitly create a connection object for your form or component. The connection object is typically created as a result of completing one of the data wizards or of dragging data objects onto your form. To connect your application to data in a database, web service, or object, run the Data Source Configuration Wizard by selecting Add New Data Source from the Data Sources Window.

The following diagram shows the standard flow of operations when connecting to data by executing a TableAdapter query to fetch data and display it on a form in a Windows application.

Data flow in a client application

In some situations, it’s convenient to create a connection object without the assistance of any data design tools. For information on creating connections programmatically, see Connecting to Data Sources.

Note

For information on connecting web applications to data, see Accessing Data with ASP.NET.

Walkthroughs for Connecting Windows Forms Applications to Data

The following walkthroughs provide procedures related to connecting to data in Windows Forms applications:

Creating Connections

In Visual Studio, connections are configured using the Add/Modify Connection dialog box. The Add Connection dialog box appears when you’re editing or creating connections within one of the data wizards or Server Explorer/Database Explorer or when you’re editing connection properties in the Properties window.

Data connections are automatically configured when you perform one of the following actions.

Action

Description

Run the Data Source Configuration Wizard.

Connections are configured when the database path is chosen in the Data Source Configuration Wizard. For more information, see How to: Connect to Data in a Database.

Run the TableAdapter Configuration Wizard.

Connections are created within the TableAdapter Configuration Wizard. For more information, see How to: Create TableAdapters.

Run the TableAdapter Query Configuration Wizard.

Connections are created within the TableAdapter Query Configuration Wizard. For more information, see How to: Create TableAdapter Queries.

Drag items from the Data Sources Window onto a form or the Component Designer.

Connection objects are created when you drag items from the Data Sources window onto the Windows Forms Designer or Component Designer. For more information, see Binding Controls to Data in Visual Studio.

Add new data connections to Server Explorer/Database Explorer.

Data connections in Server Explorer/Database Explorer appear in the list of available connections within the data wizards

Connection Strings

Connection strings can be stored within your compiled application or in the application configuration file. For more information, see How to: Save and Edit Connection Strings.

Connection Information and Security

Because opening a connection involves getting access to an important resource—a database—there are often security issues involved in configuring and working with a connection.

How you secure the application and its access to the data source depends on the architecture of your system. In a web-based application, for example, users typically get anonymous access to Internet Information Services (IIS) and therefore do not provide security credentials. In that case, your application maintains its own logon information and uses it, rather than any specific user information, to open the connection and access the database.

Security noteSecurity Note

Storing connection-string details such as a password can affect the security of your application. Using Windows integrated security is a more secure way to control access to a database. For more information, see Securing Connection Strings.

In intranet or multi-tier applications, you can take advantage of the integrated security option provided by Windows, IIS, and SQL Server. In that model, a user's authentication credentials for the local network are also used to access database resources, and no explicit user name or password is used in the connection string. Typically, permissions are established on the database server computer by means of groups, so that you do not need to establish individual permissions for every user who might access the database. In this model, you do not need to store logon information for the connection at all, and there are no extra steps required to protect connection string information.

For more information about security, see the following topics:

Design-Time Connections in Server Explorer/Database Explorer

Server Explorer/Database Explorer provides a way for you to create design-time connections to data sources. This permits you to browse available data sources; display information about the tables, columns, and other elements they contain; and edit and create database elements.

Your application does not directly use the connections available in Server Explorer/Database Explorer. These connections are used by Visual Studio for working with your database at design time. For more information, see Visual Database Tools.

For example, at design time you might use Server Explorer/Database Explorer to create a connection to a database. Later, when designing a form, you can browse the database, select columns from a table, and drag them onto the Dataset Designer. This creates a TableAdapter in your dataset. It also creates a new connection object, which is part of the newly created TableAdapter.

Information about design-time connections is stored on your local computer independently of a specific project or solution. Therefore, once you have established a design-time connection while working in an application, it appears in Server Explorer/Database Explorer whenever you work in Visual Studio, as long as the server to which the connection points is available. For more information, see How to: Connect to a Database from Server Explorer.

Note

In Visual Studio 2013, you can connect to a SQL Server database from either SQL Server Object Explorer or the Data Connections node in Server Explorer. SQL Server Object Explorer provides a view that is like SQL Server Management Studio. To open this explorer from the menu bar, choose View, SQL Server Object Explorer. For more information, see Connected Database Development.

See Also

Tasks

How to: Connect to Data in a Database

Walkthrough: Connecting to Data in a Database (Windows Forms)

Concepts

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data

Other Resources

Connecting to Data in Visual Studio

Accessing Data with ASP.NET