Using Microsoft UDL files to connect with TMSConnection

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
midiman
Posts: 2
Joined: Tue 22 May 2007 15:36
Location: USA

Using Microsoft UDL files to connect with TMSConnection

Post by midiman » Tue 22 May 2007 15:42

I am trying to connect using "FILE NAME=MyConnection.udl" as the ConnectString. (like ADO) This is on BDS2006 and with version 4.00.0.7

I get an exception message saying:

"Parameter name is unknown - FILE NAME"

...

Is there a way of loading "predefined" connections from a file?

Thanks

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Wed 23 May 2007 08:56

SDAC doesn't support such functionality.

midiman
Posts: 2
Joined: Tue 22 May 2007 15:36
Location: USA

It does now!

Post by midiman » Wed 23 May 2007 14:17

The only tricky part is to convert the UDL text file to ansi string; then remove the "irrelevant" lines. :P

Feel free to add this to your code :wink:

Code: Select all

procedure ConnectFromUDL(const aFileName: string);
type
  TUnicodeHeader = record
    Byte1: byte;
    Byte2: byte;
  end;
var
  i: integer;
  lHeader: TUnicodeHeader;
  lStringList: TStringList;
  lMemoryStream: TMemoryStream;
begin
  if not FileExists( aFileName ) then
    raise Exception.CreateFmt( 'The UDL file "%s" does not exist.', [aFileName] );
  
  lStringList := TStringList.Create;
  lMemoryStream := TMemoryStream.Create;
  try
    lMemoryStream.LoadFromFile( aFileName );

    if lMemoryStream.Size < 2 then
      raise Exception.CreateFmt( 'The UDL file "%s" is corrupt.', [aFileName] );

    lMemoryStream.Read( lHeader, SizeOf( TUnicodeHeader ));

    // check for UNICODE file
    if ( lHeader.Byte1 = $FF ) and ( lHeader.Byte2 = $FE ) then
      lStringList.Text := PWchar( longint( lMemoryStream.Memory ) + 2 )
    else
      raise Exception.CreateFmt( 'The file UDL file "%s" is not valid.', [aFileName] );

    // remove useless lines
    for i := lStringList.Count - 1 downto 0 do
      if ( lStringList[i] = '' ) then
        lStringList.Delete( i )
      else
      if lStringList[i][1] in [ '[', ';' ] then
        lStringList.Delete( i );

    MSConnection1.ConnectString := lStringList.Text;
    MSConnection1.Connect;
  finally
    FreeAndNil( lStringList );
    FreeAndNil( lMemoryStream );
  end;
end;
Last edited by midiman on Thu 24 May 2007 19:45, edited 1 time in total.

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Thu 24 May 2007 08:24

We will consider the possibility to add this functionality in the next SDAC version.

Post Reply