Page 1 of 1

Using Microsoft UDL files to connect with TMSConnection

Posted: Tue 22 May 2007 15:42
by midiman
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

Posted: Wed 23 May 2007 08:56
by Jackson
SDAC doesn't support such functionality.

It does now!

Posted: Wed 23 May 2007 14:17
by midiman
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;

Posted: Thu 24 May 2007 08:24
by Jackson
We will consider the possibility to add this functionality in the next SDAC version.