ADO (ActiveX Data Objects)

ADOExpress Step-by-Step-from-Scratch Tech Note


UDL Files — Microsoft Data Link Files

Summary: An ADO database connection is established using a Microsoft Data Link.  A Data Link is stored in a UDL file, which usually is created manually in Windows 98 or Windows 2000.  When a UDL files is not directly used, the same information is stored in the properties of a TADOConnection component in a DFM (form) file.  These instructions show how to manually or programmatically create UDL files.

A.  Background
B.  Creating a UDL File Manually In Windows 98
C.  Creating a UDL File Manually in Windows 2000
D.  Defining Data Link Properties in a TADOConnection Component
E.  Creating a UDL File Programmatically
F.  Miscellaneous Notes

A. Background

A UDL file is a Microsoft Data Link, which usually is created during development for use by the ADO to refer to a data provider. This approach is somewhat similar to an alias in the Borland BDE world. (Mastering Delphi 5, p. 40). Just like a BDE alias is used to define a database connection, a UDL file is used to define an ADO Connection.

An ADO Connection can be specified using a UDL file, or by defining the equivalent Data Link Properties in the IDE (Integrated Development Environment). When the information about an ADOConnection is hard-coded in an application by configuring the ConnectionString property of a TADOConnection component, you must recompile the program to reconfigure the connection. When this same configuration information is stored in a UDL file, the configuration can be changed at any time through Windows. So, the main advantage of using a UDL file is ease of reconfiguration.  For example, use of a UDL file allows a quick configuration change from an "absolute" filename, such as drive:\path\some.mdb, to a UNC (universal naming convention) filename, such as \\server\share\path\some.mdb.

I cannot find much documentation about Microsoft Data Links. I can’t find "Microsoft Data Link" or "UDL," in the Delphi online help, or in the Delphi 5 Developer’s Guide from Borland. Marco Cantù mentions "UDL" on just two pages in his Mastering Delphi 5 book, pp. 40 and 563, and from those pages you learn little more than "UDL" means "Microsoft Data Link."  UDL files are briefly discuss in the Delphi 5 Developer's Guide by Teixeira and Pacheco -- look on pp. 1214-1216.

The Borland ADO "BriefCase" example uses a UDL file in its ADOConnection, but doesn’t discuss what’s going on. See Program Files\Borland\Delphi5\Demos\Ado\Briefcase.

There is a some information from a MSDN search (http://msdn.microsoft.com) on "UDL," but useful information is mixed with other topics. From one MSDN article (http://msdn.microsoft.com/library/psdk/sna/db2oldb1_10iq.htm), one finds that UDL files are normally stored in a special folder located at:

Program Files\Common Files\System\OLE DB\Data Links

This MSDN article discusses how to create a UDL file in Windows 98 (or NT), which is also described in more detail in the next section.


B. Creating a UDL File Manually in Windows 98

To create a new UDL file, navigate to a folder using Windows Explorer. (Normally these files are kept in the directory:  C:\Program Files\Common Files\Systems\OLE DB\Data Links.)  Right-click in the right pane of the Windows Explorer, select New, and then Microsoft Data Link:

Immediately after creating the new UDL file, right click on it and rename it to something meaningful, such as BriefcaseDemo.UDL:

So what’s in this new BriefcaseDemo.UDL file? Using Windows Explorer, right click on the file icon pick "Properties:"

Alternately, if you double click on the file icon, the General tab is not present and the starting point is the Connection tab. When the Data Link Properties dialog appears, select the left-most tab, Provider, and select the Microsoft Jet 4.0 OLE DB Provider entry (assuming we’ll be working with a Microsoft Access database):

Press the Next button (or select the Connection tab).

Let’s assume we want to connect to the DBDEMOS Access database, which is installed as part of ADOExpess:

C:\Program Files\Common Files\Borland Shared\Data\DBDEMOS.mdb

Click on the ellipses at the right of the edit box for selecting a database name, and select this database:

Press the Open button.

Press the Test Connection button to see if the connection is working:

On the Advanced tab, only the Share Deny None option is selected by default:

A number of parameters can only be viewed and changed on the All tab:

Scroll down to view some additional parameters:


C. Creating a UDL File Manually in Windows 2000

Unfortunately the steps in Windows 2000 are slightly different than the Windows 98 instructions.

  1. Navigate to a folder using Windows Explorer, right-click in the right pane of the Windows Explorer, select New, and then Text Document.

  1. Right click on the new filename and rename it to have a UDL extension instead of TXT.

  2. Answer "Yes" to the prompt "If you change a filename extension, the file may become unusable.  Are you sure you want to change it""

  3. Double click on the new UDL file icon.  A dialog will appear with tabsheets Provider, Connection, Advanced, and All.

  4. Fill in the information as shown above in Section B.


 

D. Defining Data Link Properties in a TADOConnection Component

Start Delphi 5. Select File | Save Project As | Save In: C:\Temp | Filename: ScreenADOConnection.PAS | Save | Filename: ADOConnection.DPR | Save

From the ADO VCL tab, double click on the ADOConnection icon to put an instance of that component on the form. (Normally, a DataModule should be used to share a TADOConnection among several forms, instead of placing the TADOConnection directly on a form.)

Double click on the ADOConnection icon on the form, or select the icon and double click on the ellipses in the entry area of the Connection String property.

In the above dialog box, the name of a UDL file could be entered if the Use Data Link File radiobox option is selected. In this example, instead of specifying a UDL file, select the Use Connection String radiobox option and then the Build button. If necessary, select the Provider tab, and as above in Section 2, select the Microsoft Jet 4.0 OLE DB Provider.

This set of dialogs is the same as seen in Section 2 above.

Select the Next button, or the Connection tab, specify the following for the database name, as was done in Section 2:

C:\Program Files\Common Files\Borland Shared\Data\DBDEMOS.mdb

Select Test Connection and verify "Test connection succeeded."

Select OK and OK to close the two dialog boxes.

At this point, close Delphi 5: File | Save All | File | Close All | File | Exit

With an ASCII editor, view the ScreenADOConnection.DFM (Delphi form) file:

object Form1: TForm1
  Left = 521
  Top = 290
  Width = 156
  Height = 136
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ADOConnection1: TADOConnection
    ConnectionString =
      'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Co' +
      'mmon Files\Borland Shared\Data\DBDEMOS.mdb;Persist Security Info' +
      '=False'
    Provider = 'Microsoft.Jet.OLEDB.4.0'
    Left = 56
    Top = 32
  End
End

Note the property information specified in the Connection String in the DFM file. This information, along with default TADOConnection properties, is equivalent to the information in a UDL file.

I am not aware of any way to store this ConnectionString information in a UDL file in addition to the DFM file.


E. Creating a UDL File Programmatically

Normally a UDL file can be created directly in Windows, or the equivalent information can be specified in a TADOConnection. If for some reason you desire to create a UDL file programmatically, there is a CreateUDLFile procedure in the ADODB unit.

The CreateUDLFile procedure in the ADODB unit is rather short:

// From Unit ADODB
procedure CreateUDLFile(const FileName, ProviderName, DataSourceName: WideString);
const
  ConnStrTemplate = 'Provider=%s;Data Source=%s'; { Do not localize }
var
  ConnStr: WideString;
  DataInit: IDataInitialize;
begin
  DataInit := CreateComObject(CLSID_DataLinks) as IDataInitialize;
  ConnStr := Format(ConnStrTemplate, [ProviderName, DataSourceName]);
  OleCheck(DataInit.WriteStringToStorage(PWideChar(FileName),
  PWideChar(ConnStr), CREATE_NEW));
end;

Note that the ConnStrTemplate is used to format the Connection String.

Here is a rather complicated example of using this procedure:

USES ADODB;
...
procedure TFormCreateUDL.Button1Click(Sender: TObject);
  VAR
    FileName      : STRING;
    ProviderName  : STRING;
    DataSourceName: STRING;
begin
  FileName     := 'ProgramCreated.UDL';
  ProviderName :=
      'Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin';
DataSourceName :=
    '\\EarlG2\C\Program Files\Common Files\Borland Shared\Data' +
    '\dbdemos.mdb;Mode=Share Deny None;Extended Properties="";Locale ' +
    'Identifier=1033;Jet OLEDB:System database="";Jet OLEDB:Registry ' +
    'Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=4;J' +
    'et OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk O' +
    'ps=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database' +
    ' Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:En' +
    'crypt Database=False;Jet OLEDB:Don'#39't Copy Locale on Compact=Fals' +
    'e;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=F' +
    'alse';
  CreateUDLFile(FileName, ProviderName, DataSourceName)
end;

As seen in Section 2 above, just double click on the ProgramCreated.UDL file from Windows explorer to change any of the Data Link Properties.


F. Miscellaneous Notes

Mark Edington's (Borland) UseNet Post about DataLinkDir function
Mark Edington's (Borland) UseNet Post about advantages and disadvantages of using Data Link Files.


Links Verified 24 Jan 2000
Updated 18 Feb 2002


since 24 Jan 2000