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