Creating Data Link (UDL) Files in Delphi (Page 1 of 5 )
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.
In my last few articles I have been using ADOConnection component to connect to the database server. I have also explained the role of ConnectionString in an ADOConnection and demonstrated how to create one. The ConnectionString created during design time (like the one we created in my last article) is stored in the Form file (.dfm). Alternatively, we may decide to create it during runtime by hard coding the connection parameters.
In both instances the ConnectionString can only be modified if the source is available. This may at times be undesirable, especially if the connection string needs to be modified, for example, when distributing the binary. In circumstances where hard-coding the connection parameters are not desirable, you can always fall back on Microsoft Data Link (.UDL) files or .INI files to store connection parameters outside the application. In this article I will discuss the methods to create UDL files to store and later retrieve the information required by ADO to connect to a database server.
From information available on the Internet, one is inclined to think that Borland encourages developers to use ADO but conspicuously enough the Borland Developer Studio 2006 documentation (as well as the documentation for previous versions) says absolutely nothing about Data Link properties and creating .UDL files. However, there are features in the IDE to make it easy to create Data Link (..udl) files; we will come back to address these later in this article. Let us first create a UDL file manually and specify the Connection object to use it instead of using a hard-coded Connection String.
To create a new Data Link file, browse to the project folder or DataLink folder in explorer, right-click and from the context menu select New|Text Document. Name it Test1.txt. Rename the file to change the extension to .udl instead of .txt; the file name should now be ADOConnectProj.udl. Make sure that the extension has actually been changed; often, when you try to change the file extension from within Windows what happens is that the file name (the first part ) is changed and the desired extension is added to it while the real extension remains unchanged. This would mean that you end up with something like ADOConnectProj.udl.txt so that the file type never changes. In such circumstances I always go back to the Command prompt and use "Ren" to not only change the filename but the extension as well.
Double-click on the file to bring up the Data Link Properties dialog. The Microsoft OLEDB Provider for ODBC Drivers is selected by default in the Providers tab. Click on the Connection tab; you will require a valid ODBC DSN (Data Source Name) to connect to the database. If you haven't already created a DSN for the database, read the next section which demonstrates how to create a new DSN from the ODBC administrator in the control panel.
Next: Section 1: Adding a new ODBC Data Source >>
More Delphi-Kylix Articles
More By Danish Ahmed