Tuesday, 28 August 2012

How to use TADOTable in Delphi XE2?

Following is the code snippet which will show you how to use TADOTable in Delphi XE2?

procedure TClass1.GetDataFromADOTable;
begin
  try
    if SetADOTable then
    begin
      if tADOTable.Locate('ColumnA;ColumnB', VarArrayOf([ValueA, ValueB]), [loPartialKey])  then
      begin
        VarX := tADOTable.FieldByName('ColumnX').AsInteger;
        VarY := tADOTable.FieldByName('ColumnY').AsInteger;
      end;
    end;
  except
    on E : Exception do
    begin
      ShowMessage('Error occured in function GetDataFromADOTable: ' + E.Message);
      exit;
    end;
  end;
end;

function TClass1.SetADOTable : boolean;
begin
  try
    tADOTable.Connection := ADOConnection;  //Connectin name
    tADOTable.TableName := 'myTableName';  //Table name
    tADOTable.Open;
    result := true;
  except
    on E : Exception do
    begin
      result := false;
      ShowMessage('Error occured in function SetADOTable: ' + E.Message);
      exit;
    end;
  end;
end;

Explanation: Lets discuss the SetADOTable function first. It assings TADOTable with connection name and table name and then it opens the table. Here all data is fetched in the ADOTable from myTableName table. Now come to the function GetDataFromADOTable. It checks if there is data in ADOTable, locates a pointer to a particular column/columns by using locate keyword. Then it fetches the values in VarX and VarY.

No comments:

Post a Comment