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.
In some situations, you might find it convenient to create a connection object without the assistance of any data design tools. For information on creating connections programmatically, see Connecting to a Data Source (ADO.NET).
Note
For information on connecting Web Applications to data, see ASP.NET Data Access.
Walkthroughs for Connecting Windows Forms Applications to Data
The following lists topics related to connecting to data in Windows Forms applications.
Creating Connections
When using Visual Studio, connections are configured using the Add/Modify Connection Dialog Box (General). The Add Connection dialog box appears when you are editing or creating connections within one of the data wizards or Server Explorer/Database Explorer or when you are 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. |
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 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 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 Protecting Connection Information (ADO.NET). |
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 pages:
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
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