2008. október 25., szombat

Using UDL files to simplify ADO


Problem/Question/Abstract:

How do I get the ADO connection dialog up in Delphi

Answer:

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 unit for displaying the ADO configuration dialogs, I found another way. 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 haven’t 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 I’ve 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.

Nincsenek megjegyzések:

Megjegyzés küldése