Ask a Question

How to load the contents of a file from the store into a string.

bravecobra
New Contributor

How to load the contents of a file from the store into a string.

Hi,



I'm trying to figure out how to load the content of a file into a string in DelphiScript.

The idea is to load a SQL script (that builds my database/ imports sample data) from the stores and then execute it through a ADO connection.

I can execute individual statements, but not whole script.That's what I'm after..



Here's what I've got so far, however TStringlist is not known. (AConnection is a global variable specifying an ADO connection to the server).




procedure ExecuteSQLScript(ASQLScript:string);

var AScript: TStringList;

begin  

  try

    AScript := TStringList.Create;

    try

      AScript.LoadFromFile(Objects.StoredObject[
<meta http-equiv="content-type" content="text/html; charset=utf-8" />ASQLScript]);

      AConnection.Execute_(AScript.Text);

      Log.Message(Format('Executed SQL: %s',[
<meta http-equiv="content-type" content="text/html; charset=utf-8" />AScript.Text]));

    finally

      AScript.free;

    end;         

  except

    Log.Error('Execption',ExceptionMessage);

  end;

end;


Any ideas?

Tnx.
3 REPLIES 3


Hi Brave,





Please note that DelphiScript and Delphi are different languages, and many Delphi features are unavailable in its script version. Please find more information in the DelphiScript Description help topic.





You can read the content of a text file into a variable using the aqFile object (see the aqFile Object topic).







procedure test;

const

  fileName = 'c:\textfile.txt';

var

  textVar;  

begin

  textVar := aqFile.ReadWholeTextFile(fileName, aqFile.ctANSI);

  Log.Message('See the file content in the Remarks pane', textVar);

end;
--
Dmitry Nikolaev

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
bravecobra
New Contributor

I must have been confused by this part of the help: Working with string lists.



Thus, that means that I have to save the file from the store to disk first and then load it from there...I just hoped I could load the content straight on, without the round trip to the disk.



Tnx. 


Hi Brave,





Files in the Stores collection are actually stored on a disk. You can get their path by using the FileNameByName method.







procedure test;

var

  fileName,

  textVar;  

begin

  fileName := Files.FileNameByName('myQuery.sql');

  textVar := aqFile.ReadWholeTextFile(fileName, aqFile.ctANSI);

  Log.Message('See the file content in the Remarks pane', textVar);

end;
--
Dmitry Nikolaev

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
cancel
Showing results for 
Search instead for 
Did you mean: