Partilhar via


Visual Basic Concepts

Connection Objects

To access data using your Data Environment, you must create a Connection object. Therefore, every Data Environment should include at least one Connection object. A Connection object represents a connection to a remote database that is used as a data source.

Upon adding a Data Environment to your Visual Basic project, the Data Environment designer automatically includes a new connection, called Connection1. At design time, the Data Environment opens the connection and obtains metadata from the connection, including database object names, table structures, and procedure parameters.

Note   If Show properties immediately after object creation is selected in the Options dialog box, the Data Link Properties dialog box will appear when you add a Data Environment to your project. This option is not selected by default.

Creating a Connection Object

The Add Connection function is available at all times and is independent of the existence of other objects.

To create a database connection

  • Click Add Connection on the Data Environment designer toolbar.

    -or–

    Right-click your Data Environment designer and select Add Connection from the shortcut menu.

    Once you have added a Connection, the Data Environment is updated to show the new Connection object. The default name for this object is "Connection," followed by a number, such as Connection1.

    Use the following procedure to specify Connection object properties.

To set the Connection Name and Data Source

  1. In the Visual Basic Properties window, change the default Name to a more meaningful name for your data source database. For example, you may wish to change Connection1 to "Northwind" if the data source is the Northwind database.

  2. Right-click the Connection object and choose Properties to access the Data Link Properties dialog box.

  3. From the Data Link Properties dialog box, specify the connection information on the Provider and Connection tabs. This is typically a database that contains data or stored procedures. You may select only one source for each Connection object.

    Note   Regardless of the selected data source type, the Data Environment accesses all data via ADO and OLE DB interfaces.

  4. Click OK to apply the properties and close the dialog box.

Setting Logon Information

Logon information only needs to be supplied if the database being accessed via the Connection object requires authentication information. You can specify a different set of logon information to be used at design time and run time. For example, you might want to develop your application using a system administrator user identification and password, but supply a general guest user identification when the application is run.

Note   Any logon information specified on the Data Link Properties dialog box is overridden by logon information specified on the Visual Basic Properties window.

To specify logon information

  1. On the Visual Basic Properties window, specify the user identification and password to be used at and , if necessary. You can also specify the prompt behavior.

  2. Set DesignSaveAuthentication to True if you want the specified information to persist for design time. This information is not written into the built executable file or dynamic-link library (DLL). If this option is False, any information in DesignUserName and DesignPassword is lost once you close and re-open the project.

  3. Set RunSaveAuthentication to True if you want the specified authentication used at run time. The authentication information is stored in the properties of the class and persists in the built executable file or DLL. If this option is False, any information in RunUserName and RunPassword is lost once you close and re-open the project.

    Note   Since the password is not encrypted, for maximum security you should not specify your password to persist for run time or design time.

Setting Miscellaneous Connection Information

The miscellaneous connection information consists of advanced options that change how the database is accessed.

To specify miscellaneous information

  1. On the Visual Basic Properties window, specify the following:

    • The CursorLocation to be used for the connection. For information on cursors, see .

    • Additional parameters for the connection string in ConnectionSource. Any string placed here is appended to the ConnectionString****property when connecting to the data source. For example, Database=pubs.

  2. Right-click the Connection object and choose Properties to access the Data Link Properties dialog box.

  3. On the Advanced and All tabs, you can:

    • Specify the number of seconds for the connection and command timeouts.

    • Indicate permissions and network settings.

  4. Click OK to apply the properties and close the Data Link Properties dialog box.

    Note   A Connection object is not opened until information is needed from the connection, such as a list of tables. When a Connection object is selected in the Data Environment outline view, the status bar text can be used to determine if the connection is currently established (open).

Dragging From a Data View to Your Data Environment

You can automatically create Connection objects by dragging a connection from the Data View window to your Data Environment designer. This is an easy and efficient way to create Connection objects that already exist in your Data View.