Search code examples
delphipascaltdataset

Avoiding Duplicate Column Name Additions in a TDataSet


I am dynamically adding fields to a TDataSet using the following code:

while not ibSQL.Eof do
   fieldname := Trim(ibSql.FieldByName('columnnameofchange').AsString);
   TDataSet.FieldDefs.Add(fieldname , ftString, 255);
end

Problem is that I might get duplicate names so what is the easiest way to screen for duplicates and not add the duplicates that are already added.

I hope not to traverse through the TDataSet.FieldDefList for each column added as this would be tedious for every single column addition. And there can be many additions.

Please supply another solution if possible. If not then I am stuck using the FieldDefList iteration.

I will also add that screening out duplicates on the SQL query is an option but not a desired option.

Thanks


Solution

  • TFieldDefs has a method IndexOf that returns -1 when a field with the given name does not exist.