Adding items dynamically in Clientdataset

Asked

Viewed 178 times

0

How can I put the text of a TMemo separated by semicolons (;) as records in TClientDataSet.

Example:

Code;Category;Phrase;Author

1;Motivational;Sentence 1;Author 1

2; Proverb; Sentence 2; Author 2

3;Dictation; Sentence 3;Unknown

What I find more complicated is to "cut out" the String at each semicolon(;) and read it later.

I believe the code will follow that logic:

for Linha := 0 to Memo1.Lines.Count-1 do
begin
  CDS.Insert;
  CDSCod.AsString       := {Código}
  CDSCategoria.AsString := {Categoria}
  CDSFrase.AsString     := {Frase} 
  CDSAutor.AsString     := {Autor}
  CDS.Post;
end;

1 answer

4


You can use the String helper for this, declare in the uses a System.StrUtils. This way you have access to various help functions.

For the case presented you would use:

CDS.Insert;
CDSCod.AsString       := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDSCategoria.AsString := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDSFrase.AsString     := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDSAutor.AsString     := Memo.Lines.Strings[x].Split(';')[posicao_especifica]
CDS.Post;

If the dataset in question that is using has no set index you can change the .Insert for .Append to win in performance.

Edit: Remembering that as String is an array, posicao_especifica starts with 0.

  • Gave error here "There is no overloaded version of 'Split' that can be called with These Arguments"

  • Helper version, modify to Split([';']) the one you have expects an array of char.

  • I had already tried to use Split([';']) but keeps making a mistake.

  • Can you give an example, this is too basic not to work.

  • Now it worked here. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.