Using a Microsoft Data Link (.udl) File to Provide Persistent Connection Information

Goal

You want your application to reference connection information that is stored in a Microsoft Data Link (.udl) file. The .udl file is a special text file that can be used in conjunction with ADO and the Visual Basic DataEnvironment tool in order to initialize an ADO Connection object. (This connection method is similar to the method that is provided for ODBC by the ODBC Administrator.)
Note: DataEnvironment is a Visual Basic 6 GUI tool that can be used to manage static connections to known data sources. To add it to your project, select Projectthen selectAdd Data Environment.
This recipe applies to all the SAS providers. This recipe applies only to ADO. Sample code is included.
Note: To use the sample code, you must reference the Microsoft ActiveX Data Objects Library in your Visual Basic project.

Implementation

Creating a Microsoft Data Link (.udl) File

To create a .udl file:
  1. Create an empty text file with the extension .udl.
  2. From Windows Explorer, double-click on your new file to open the Data Link Properties dialog box.
    Note: The Data Link Properties dialog box is the same interface that the user sees when you create an application that uses the MSDASC.DataLinks object (see Prompting Users for Connection Information by Displaying the Data Link Properties Dialog Box).
  3. In the Data Link Properties dialog box, enter the connection information that is required to open an ADO Connection object.
  4. Click OK to save the .udl file with the information that you entered.

Sample Code That Shows How to Reference the File

The following sample code shows you how to reference the file in the ConnectionString property of a Connection object. It requires that you reference the Microsoft ActiveX Data Objects Library.
   Dim obConnection As New ADODB.Connection

   obConnection.ConnectionString = "File Name=c:\mydir\myfile.udl"   
   obConnection.Open
Note: You can also refer to the .udl file in the Visual Basic DataEnvironment tool.
Tip
If you store the .udl file on a server, you can update the .udl file, instead of your application code, when a data source is moved to a new location.