TMyConnection.GetFieldNames

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jouniaro
Posts: 6
Joined: Fri 08 Jul 2011 08:42

TMyConnection.GetFieldNames

Post by jouniaro » Fri 08 Jul 2011 08:52

Hi, I am evaluating your components, because we have odd problems with TADOConnection to mysql.

But our system requires a way to get the field names for any table, using

TMyConnection.GetFieldNames(TableName: string;
List: TStrings);

similar to TAdoConnection.

Any help available? We are in a hurry since our final tests are blocking now due to this strange issue with ADO/MySQL, which only appears in the final system and not in any of our test installations, so I am looking for any replacement ASAP.

AndreyZ

Post by AndreyZ » Fri 08 Jul 2011 12:58

Hello,

For the time being, MyDAC doesn't have such functionality. We will investigate the possibility of adding this functionality in the future. You can use the TMyMetaData component to obtain the same result. Here is an example:

Code: Select all

procedure GetFieldNames(con: TMyConnection; TableName: string; List: TStrings);
var
  md: TMyMetaData;
begin
  md := TMyMetaData.Create(nil);
  try
    md.Connection := con;
    md.MetaDataKind := 'Columns';
    md.Restrictions.Values['TABLE_SCHEMA'] := md.Connection.Database;
    md.Restrictions.Values['TABLE_NAME'] := TableName;
    md.Open;
    List.Clear;
    while not md.Eof do begin
      List.Add(md.FieldByName('COLUMN_NAME').AsString);
      md.Next;
    end;
  finally
    md.Free;
  end;
end;

jouniaro
Posts: 6
Joined: Fri 08 Jul 2011 08:42

Post by jouniaro » Sat 09 Jul 2011 20:07

Thanks, I managed to write the same using a query to the information_schema database (before your reply):

Code: Select all


  if Connection.Connected then
  begin
    C := TMyConnection.Create(nil);
    Q := TMyQuery.Create(nil);
    try
      C.Server := Connection.Server;
      C.Username := Connection.Username;
      C.Password := Connection.Password;
      C.Database := 'information_schema';
      Q.Connection := C;
      C.Connect;
      Q.SQL.Text := Format(
        'select column_name from columns where table_schema=''%s'' and table_name=''%s''',
        [Connection.Database, TableName]);
      Q.Open;
      List.Clear;
      while not Q.Eof do
      begin
        List.Add(VarToStr(Q.FieldValues['column_name']));
        Q.Next;
      end;
    finally
      Q.Free;
      C.Free;
    end;
  end;
It solved my problem, so I will probably go on ordering a license soon...

AndreyZ

Post by AndreyZ » Mon 11 Jul 2011 11:41

To obtain field names, the TMyMetaData component executes a query to the information_schema database too. Therefore you can use any of these variants.

jouniaro
Posts: 6
Joined: Fri 08 Jul 2011 08:42

Post by jouniaro » Mon 11 Jul 2011 11:58

That's what I thought too. Unfortunately my Delphi XE help is broken and I could not restore it - but I downloaded the Delphi 7 version as well to get your help...

It seems TMyMetadata is in the pro package, so I won't need that :)

AndreyZ

Post by AndreyZ » Mon 11 Jul 2011 13:42

You can also download the SDAC documentation in the CHM format here: http://www.devart.com/sdac/sdacchm.zip

Post Reply