Pages

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

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.