HOWTO: Use Data Link Files with ADO 2.0

ID: Q189680


The information in this article applies to:
  • ActiveX Data Objects (ADO), versions 2.0, 2.1 SP2
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0


SUMMARY

ODBC includes the ability to save Data Source Name (DSN) information (which basically amounts to persisted connection strings) in a simple file rather than in the registry.

ActiveX Data Objects (ADO) 2.0 provides similar functionality in the form of a Data Link file (*.udl).


MORE INFORMATION

Creating a Data Link File

  1. Right-click in the directory where you want to store the file and then click New, Microsoft Data Link. A new file appears in the directory with the name New Microsoft Data Link.udl. Rename the file to Pubs.udl.


  2. Double-click the newly created file to set the properties for the Data Link. You will see the following tabs:


  3. Provider | Connection | Advanced | All
  4. The Provider tab allows you to specify the provider (the default is Microsoft OLE-DB provider for ODBC drivers). Use the Default provider and then click the NEXT button.


  5. The Connection tab allows you to specify either the data source name designated in the ODBC Administrator or allows you to provide the connection string.

    Here are two ways you can provide information:

    • Use a predefined DSN name for the connection (it has all the information to connect to the Data source)


    • Build a connection String or use a known connection string.


    Assuming that you have already created a System DSN for the pubs database, you should see the DSN in the drop-down list box (Use Data source). Choose the DSN for Connection and test it by clicking the Test Connection button. You can also build your connection strings.


  6. Click OK to complete the settings. To learn more about Data Link files, please see the Help file, Msdasc.hlp Found in the C:\Program Files\Common Files\System\ole db directory.


  7. Open a text editor, such as Microsoft Notepad, and drag the DataLink icon onto a blank document. You will see the connect string that the data link represents. You can use this connect string in your code.


Using a Data Link File in Code with an ADO Connection

Using a Data Link file for an ADO connection is fairly simple. Here is the syntax:

cnPubs.ConnectionString = "File Name=C:\Path\To\Pubs.UDL;" 
You can also use a relative path as shown in the following example:

cnPubs.ConnectionString = "File Name=Pubs.UDL;" 
-or-
cnPubs.ConnectionString = "File Name=Data\Pubs.UDL;" 
If you use a relative path, then ADO looks for the Data Link file relative to the directory where the application is installed.

Using a Data Link File for a DataEnvironment Connection

The DataEnvironment that is part of Visual Basic 6.0 does not have an option for using Data Link files in the user interface for connecting to your database. To use the Data Link file, cancel out of the dialog boxes that ask you to specify an ODBC DSN or OLE DB provider. Use the ConnectionSource property for the DataEnvironment connection in the Visual Basic Properties window and enter a connection string, as shown in the preceding example, to let the DataEnvironment know where your Data Link file is located.


REFERENCES

For additional information, please see the following World Wide Web URLs:

http://www.microsoft.com/support/
http://www.microsoft.com/data/ado/

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by David Sceppa, Microsoft Corporation

Additional query words:

Keywords : kbADO200 kbDatabase kbVBp500 kbVBp600 kbGrpVBDB kbGrpMDAC kbDSupport kbVBp600FAQ kbADO210sp2
Version : WINDOWS:2.0,2.1 SP2,5.0,6.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.