This documentation is archived and is not being maintained.

Creating ADO.NET Connection Objects

Visual Studio .NET 2003

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. However, in some instances you might find it convenient to create a connection alone.

Your options for creating a connection are:

  • Use any of the following data design tools, which create a connection object as part of the task they are performing:
    • Data Adapter Configuration Wizard - this wizard prompts you for information to create a connection that is in turn linked to a data adapter. For more information, see Data Adapter Configuration Wizard.
    • Data Form Wizard - this wizard creates the connection object as part of the form it is configuring. For more information, see Data Form Wizard.
  • Drag a table, selection of columns, or stored procedure from Server Explorer onto a form or component. By dragging one of these elements onto a form, you create both a data adapter and a connection. For more information, see Adding New Data Connections in Server Explorer.
  • Create a standalone connection. This option creates a connection object on your form or component whose properties you configure manually. This strategy is useful if you intend to set connection properties at run time or if you simply prefer setting the properties in the Properties window. The procedure is described below.
  • Create a connection programmatically. For more information about creating and managing connections in code, see Connecting to SQL Server Using ADO.NET and Connecting to an OLE DB Data Source Using ADO.NET.

To create a connection

  1. From the Data tab of the Toolbox, drag a connection object onto your form or component. For more information about which connection object to use, see Introduction to ADO.NET Connection Design Tools.
  2. Select the connection in the designer and then use the Properties window to set its connection string.
    • You can set the ConnectionString property as a single unit. (You cannot specify a Provider attribute for a SqlConnection object.)

      – or –

    • You can set individual properties (DataSource, Database, UserName, and so on). If you set individual properties, the connection string is built for you.
      Security Note   Storing connection-string details (such as the server name, user name, and password) can have implications for the security of your application. Using Windows Integrated Security is a more secure way to control access to a database. For more information, see Database Security.
  3. If you want to rename the connection object, set its Name property.
    Note   If connection information needs to be set at run time, you can configure connection properties as dynamic properties. For more information, see Configuring Applications Using Dynamic Properties.
  4. If you want to set properties at run time without recompiling the application, specify connection properties. For more information about making connection properties dynamic, see Introduction to ADO.NET Connection Design Tools. For an overview of dynamic properties, see Configuring Applications Using Dynamic Properties.

See Also

Creating Connection to SQL Servers | Creating Connections to Access Databases | Creating Connections to ODBC Data Sources | Creating Connections to Oracle Databases | Introduction to ADO.NET Connection Design Tools | Connecting to a Data Source Using ADO.NET | Connection Pooling for the .NET Data Provider for SQL Server

Show: