Data Link Files
So a TADOConnection allows us to centralize the definition of a connection string within a form or data module. However, even though this is a worthwhile step forward from scattering the same connection string throughout all ADO datasets, it still suffers from a fundamental flaw: If you use a database engine that defines the database in terms of a filename, then the path to the database file(s) is hard-coded in the EXE. This makes for a very fragile application. The BDE uses aliases to overcome this problem; ADO uses Data Link files. A Data Link file is a connection string in an INI file. The following is an example of a Data Link file: [oledb]
; Everything after this line is an OLE DB initstring
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\program files\microsoft office\ office\samples\northwind.mdb;Persist Security Info=False
Although you can give a Data Link file any extension, the recommended extension is .UDL. You can create a Data Link using any text editor, or you can right-click Windows Explorer, select New, then Text Document, rename the file with a UDL extension, and then double-click the file to invoke the Microsoft connection string editor.
To use the Data Link file, set the TADOConnection's ConnectionString to: File Name=TEST.UDL
assuming that the file is called TEST.UDL and it is in the same directory as the EXE. You can place your Data Link files anywhere on the hard disk, but if you are looking for a common, shared location, then you can use the DataLinkDir function in ADODB.PAS: ShowMessage('The Data Link directory is ' + DataLinkDir); If you haven't altered MDAC's defaults, DataLinkDir will return C:\Program Files\Common Files\System\OLE DB\Data Links
Delphi 5's ADOExpress suffers from a flaw when using data link files, which is fixed in Delphi 6's dbGo. In 5, set the Connected property of the TADOConnection to True and watch the ConnectionString property. The ConnectionString becomes the actual connection string that is in use (i.e., the one from the data link file). The problem is that when the property is streamed to the form when the application is saved, it is the active connection string that is saved. The reference to the data link file is permanently forgotten. If you change the data link file, you will not see any change to your application.
If you want to use data link files and you don't want to suffer from this problem, use the following TADOConnectionX component instead of TADOConnection:
TADOConnectionX = class(TADOConnection) private
FUDLFile: string; protected procedure DoConnect; override; published property UDLFile: string read FUDLFile write FUDLFile; end;
The component has a UDLFile property where the UDL filename is permanently stored (you must manually set this property yourself). The class has a single method, DoConnect, which ensures that the UDL file is always read each time the connection is opened:
procedure TADOConnectionX.DoConnect; begin if FUDLFile <> " then
ConnectionString:= 'File Name=' + FUDLFile; inherited; end;
Post a comment