Home arrow Delphi-Kylix arrow Page 4 - Creating Data Link (UDL) Files in Delphi
DELPHI-KYLIX

Creating Data Link (UDL) Files in Delphi


The connection string required to connect to a database server may be created during design time or run time. There are circumstances under which you may want to store the connection string entirely outside the application, however. In those instances, Microsoft Data Link files are useful, as this article will explain.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
August 06, 2007
TABLE OF CONTENTS:
  1. · Creating Data Link (UDL) Files in Delphi
  2. · Section 1: Adding a new ODBC Data Source
  3. · Section 2: Connecting with SQL Server Drivers
  4. · Section 3: Creating Data Link files programmatically
  5. · Section 4: More undocumented routines in ADODB

print this article
SEARCH DEVARTICLES

Creating Data Link (UDL) Files in Delphi - Section 3: Creating Data Link files programmatically
(Page 4 of 5 )

As I said in the introductory lines, Delphi has good support for ADO but the documentation says absolutely nothing about Data Link files. This becomes more confounding when you realize that there are functions in the Delphi library that make creating UDL files programmatically a cakewalk. To find out about them, open the unit ADODB.pas in Delphi code editor. You will find several helper routines that are not documented in the Delphi Help file. One of these helper routines is the CreateUDLFile routine which takes three parameters: the name of the UDL File to be created, the provider name and the name of the data source.

procedure CreateUDLFile(const FileName, ProviderName, DataSourceName: WideString);

Using this method, it is possible and pretty easy to create a UDL file which can be used to replace a hard-coded connection string in an application. After specifying the name of the UDL file and the provider we can concatenate the connection parameters like user id and password with the data source name and pass it as the parameter required by the CreateUdlFile method.

CreateUDLFile(ADOConnectProj.udl','SQLOLEDB.1','MyServer;'+ 'Password=myPass;' +'Persist Security Info=True;'+ 'User ID=sa;' + 'Initial Catalog=TestDB;');

Delphi libraries contain yet another undocumented routine: DataLinkDir, which gets from the registry the default path to save UDL files. This information can be used in the above function to create the file in the DataLinks folder. All we need to do is append the intended UDL file name in the constructor method with the DataLinkDir routine; the file can then be created and stored in the default folder instead of the root directory or any other folder.

CreateUDLFile(DataLinkDir + 'ADOConnectProj.udl' , 'SQLOLEDB.1' , 'MyServer; ' + 'Password=myPass;' +'Persist Security Info=True;' + 'User ID=sa;' + 'Initial Catalog=TestDB;' );

To read the Connection String from the UDL file we can assign the filename to the ConnectionString property of ADOConnection1.

ADOConnection1.ConnectionString:= 'FILE NAME=' + DataLinkDir + 'ADOConnectProj.udl';

The following code snippet demonstrates how to create a UDL file and use it to read the Connection String and connect to a Database server while using ADO:

if not FileExists(DataLinkDir + 'ADOConnectProj.udl') then
 
begin
   
CreateUDLFile(DataLinkDir + 'ADOConnectProj.udl','SQLOLEDB.1','MyServer;'+ 'Password=myPass;' +'Persist Security Info=True;'+ 'User ID=sa;' + 'Initial Catalog=TestDB;');
 
end;
ADOConnection1.ConnectionString:= 'FILE NAME=' + DataLinkDir + 'ADOConnectProj.udl';
ADOConnection1.Open;
if ADOConnection1.Connected then
 
ShowMessage('Successfully connected');

Being able to programmatically create UDL files and let the connection string be loaded from the file provides a very flexible approach to making ADO connections. As a programmer, you can allow the user to provide information during installation and let the the installation program create a UDL file that can be used later for server connections. Further, changing the connection string would not require recompilation since the UDL file can be executed to launch the Data Link property dialog to change the connection string. You can also let it create an interface to allow the user to specify parameters to be used to create the connection string and connect to the server.


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials