How to Use Delphi or C++ Builder to Run a Query in a Text file

By: Borland Staff

Abstract: The LoadFromFile method can Load a query from Disk

Problem:
I would like to run a query saved in a text file using Delphi or C++ Builder.  How do I do that?

Solution:


Step by Step Instructions on How to Run a Query from a File Using Delphi
===========================================================


1.    Create an alias in the BDE to point to the database.

2. Create a file called 'myquery.sql' in your c: root directory that contains the following line:
                               select * from employee

3. Start Delphi.

4. File | New Application

5. Place a button on the form. You can do this by clicking on 
   the button icon on the Standard page of the component palette
   and then clicking on the form.

6.  Click on the Data Access tab of the component palette.

7.  Click on TQuery icon and then on the form to place a  TQuery on the form.

8.  Hit  to bring up the Object Inspector.

9. Define the TQuery's DatabaseName property to be the alias that points to the employee database.

10. Click on DataSource icon and then on the form to place a DataSource on the form.

11.  Hit  to bring up the Object Inspector.

12.  Define the DataSet property of the DataSource object to be Query1.

13.  Click on the Data Controls tab on the component palette.

14. Click on DBGrid icon and then on the form to place a DBGrid on the form.

15.  Hit  to go back to the Object Inspector.

16.  Define the DBGrid's DataSource property to be DataSource1.

17. Click on the button again.

18.  Hit  to go to the Object Inspector.

19.  Click on the 'Events' tab.

20. Double Click in the white space next to the OnClick event.

21. Place the following code in the button's OnClick event code:
                Query1.Close;
                Query1.SQL.Clear;
                Query1.SQL.LoadFromFile('C:MYQUERY.SQL');
                Query1.Open;

22. Run the application by hitting .

23. Press the button to see the query run.


Note:  You can also read a query into the TQuery object with SQL Builder.
            Use the File | Import from file menu in SQL Builder choices to do this.
            For more information about accessing SQL Builder, please refer
            to document number  25679.



Server Response from: ETNASC04