I am using this code to run my sql scripts in inno setup. However ADO gives an error if the script contains GO syntax within it. I added my own code to overcome this problem.
My code below splits the script at each GO statement and run each section separately. I think this could be done in better ways.
[code]
const
adCmdText = $00000001;
adExecuteNoRecords = $00000080;
procedure RunScript();
var
tString: TStringList;
ADOCommand: Variant;
ADORecordset: Variant;
ADOConnection: Variant;
i, len: Integer;
strToQuery, str: String;
begin
try
ADOConnection := CreateOleObject('ADODB.Connection');
ADOConnection.ConnectionString :=
'Provider=SQLOLEDB;' + // provider
'Data Source=(local)\SQL2000;' + // server name
'Initial Catalog=databasename;' + // default database
'User Id=userId;' + // user name
'Password=password;'; // password
ADOConnection.Open;
try
ADOCommand := CreateOleObject('ADODB.Command');
ADOCommand.ActiveConnection := ADOConnection;
tString:= TStringList.Create;
tString.LoadFromFile('C:\sqlscript.sql');
len:= tString.Count;
strToQuery:='';
for i:=0 to len-1 do begin
str := trim(tString[i]);
if POS('go', lowercase(str)) = 1 then begin
//If GO is found run the script added in to strToQuery since last GO
ADOCommand.CommandText := strToQuery;
ADOCommand.Execute(NULL, NULL, adCmdText or adExecuteNoRecords);
strToQuery:=''; //Clear the string to start adding till next GO
end else strToQuery:= strToQuery +#13#10+ str; //Add the script to strToQuery
end;
ADOCommand.CommandText := strToQuery;
ADOCommand.Execute(NULL, NULL, adCmdText or adExecuteNoRecords);
finally
ADOConnection.Close;
tString.Free;
end;
except
MsgBox(GetExceptionMessage, mbError, MB_OK);
end;
end;
Any suggestion to update the code will be much appreciated.
Inno Setup
. \$\endgroup\$