Delphi 7 Execute SQL script file directly in the program

Source: Internet
Author: User

Delphi 7 Execute SQL script file directly in the program

In some operations that handle MSDE. Some SQL scripts need to be executed. Some from
SQL Server 2000 generates a script with a suffix. There is no Enterprise Manager in MSDE,
The operation is done in the program. So use the following function to execute the SQL script.

Executes a SQL corner of this file, the file can only be ANSI encoded.
If the file is Unicode encoded, it will be garbled.
Var
s:string;
sqltext:string;
Sqlfile:textfile;
Begin
If Opendialog1.execute Then
Begin
AssignFile (Sqlfile, opendialog1.filename);
FileMode: = 0;
Reset (Sqlfile);
Try
Adoconnection1.begintrans;
While not EOF (Sqlfile) does
Begin
READLN (Sqlfile, s);
Sqltext:=s;
while (not EOF (Sqlfile)) and
(Uppercase (Trim (s)) <> ' GO ') do
Begin
READLN (Sqlfile, s);
if (Trim (s)) <> ' GO ') then uppercase
Sqltext:=sqltext+ ' +s;
End
Adoquery1. Close;
Adoquery1. Sql. Clear;
Adoquery1. Sql. ADD (SQLTEXT);
Adoquery1. Execsql;
End
CloseFile (Sqlfile);
Adoconnection1.committrans;
Application. MessageBox (' SQL Corner book complete! ‘,
' Hint ', mb_ok+mb_iconinformation);
Except
Raise exception. Create (' SQL corner of this execution failed! ');
Adoconnection1.rollbacktrans;
End
End
end;

Where: Adoconnection1,adoquery1,opendialog1 are the controls placed in the window. It can be set as a local variable, created and destroyed within this function.

Delphi 7 Execute SQL script file directly in the program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.