Mega Code Archive

 
Categories / Delphi / Files
 

Delphi 4 Using UDL files to simplify ADO

Title: Delphi 4 - Using UDL files to simplify ADO Question: How do I get the ADO connection dialog up in D4 Answer: Ado On Delphi 4- UDL Files to simplify deployment. Recently I had to do some work with SQL Server 7 using D4. To minimise the need for ODBC configuration, I chose to use ADO. As D4 lacks the Adoconed (see article 1413) unit for displaying the ADO configuration dialogs, I found another way. Some of this code is derived from Marcus Neves article 1492 but there is some new stuff. If the UDL file is absent or corrupted, it displays the dialog then creates a new file. If however an existing configuration file is there then it loads it in and uses it. For the ADO proper (which I havent shown- this just sets up the Ado connection string) I used the Ado components from http://www.alohaoi.com which are freeware with source and the best Ive found. Just install the Ado components then put a button on the form to test this below. For anyone that is interested, the Msdasc objects (which manage the connection dialogs) are contained in oledb32.dll- import the type library to get access to this. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,aoADODB_tlb,aomsdasc_tlb, aoADODB,ComObj; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private procedure loadudl; procedure NewLink; { Private declarations } public ObjDataLink : Datalinks; dbConnection : connection; DataInitialize : IDataInitialize; WUdlFile : Widestring; AdoStr : string; { Public declarations } end; var Form1: TForm1; implementation Const UdlFile ='Adolink.udl'; procedure Tform1.loadudl; var pwstr: pwidechar; wUDLFile: array[0..MAX_PATH - 1] of WideChar; begin DataInitialize := CreateComObject(CLASS_DataLinks) as IDataInitialize; StringToWideChar (UDLFile, @wUDLFile, MAX_PATH); if Failed(DataInitialize.LoadStringFromStorage(wUDLFile, pwstr)) then begin ShowMessage('Link file corrupted or missing- please renew'); Newlink; end else begin adostr := pwstr; end; end; procedure Tform1.NewLink; var str: widestring; wUDLFile: array[0..MAX_PATH - 1] of WideChar; begin str := ''; ObjDataLink := Codatalinks.Create; if adostr '' then begin dbconnection := coconnection.create; dbconnection.ConnectionString := adostr; if ObjDataLink.PromptEdit(idispatch(dbconnection)) then str := dbconnection.ConnectionString; end else begin dbconnection := ObjDataLink.PromptNew as _connection; if assigned(dbconnection) then str := dbconnection.ConnectionString; end; DataInitialize := CreateComObject(CLASS_DataLinks) as IDataInitialize; StringToWideChar (UDLFile, @wUDLFile, MAX_PATH); sysutils.DeleteFile(udlfile); if Failed(DataInitialize.WriteStringToStorage(wUDLFile, pwidechar(Str), CREATE_NEW)) then raise Exception.Create('Can''t write UDL to '+udlfile); adostr := str; end; {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); begin LoadUdl; end; end.