procedure TThrUpdateDatabase.Execute;
const
MaxScripts = 5;
Scripts: Array[1..MaxScripts] of string = ('update.sql', 'constraints.sql', 'procedures.sql', 'views.sql', 'triggers.sql');
var
oConnection : TADOConnection;
RecordsAffected: Integer;
Script, Script2, AllScripts: TStringlist;
Ok, DoScript: Boolean;
I, N: Integer;
function Prog(Index: Integer): Integer;
begin
Result := Index + 1;
if Result > MaxScripts then
Result := MaxScripts + 1;
end;
begin
CoInitialize(Nil);
try
oConnection := TADOConnection.Create(Nil);
Ok := True;
try
if not Terminated and Ok then
begin
AllScripts := TStringlist.Create;
try
for I := 1 to MaxScripts do
AllScripts.Add(Scripts[I]);
if FSpecialScripts <> Nil then
AllScripts.AddStrings(FSpecialScripts);
oConnection.ConnectionString := 'Provider=SQLOLEDB;Data source='+Server+';' + 'Initial catalog='+DBName;
oConnection.LoginPrompt := False;
oConnection.Open(CIMSQLAdmin, CIMSQLPassword);
oConnection.CommandTimeout := 600;
try
Script := TStringlist.Create;
Script2 := TStringlist.Create;
try
For N := 0 to AllScripts.Count-1 do
begin
Script.Clear;
Script2.Clear;
try
if not Terminated and Ok then
begin
DoScript := True;
if (AllScripts[N] = 'constraints.sql') and not DoConstraints then
DoScript := False;
if DoScript then
begin
Script.LoadFromFile(Dir + '\' + AllScripts[N]);
Script2.Clear;
PostMessage(Receiver, wm_SetProgress, Prog(N), Script.Count);
for I := 0 to Script.Count-1 do
begin
if not Terminated then
begin
if (UpperCase(Trim(Script[I])) = 'GO') or (I = Script.Count-1) then
begin
if Trim(Script2.Text) <> '' then
try
oConnection.Execute(Script2.Text, RecordsAffected, []);
except
on E: Exception do
if MessageDlg('Update error: op lijn ' + IntToStr(I) + #13 + E.Message+ #13+
'Text: ' + Script2.Text,
mtError, [mbIgnore, mbCancel], 0) = idCancel then Exit;
end;
Script2.Clear;
end else
begin
Script2.Add(Script[I]);
if I = Script.Count - 1 then
try
oConnection.Execute(Script2.Text, RecordsAffected, []);
except
on E: Exception do
if MessageDlg('Update error: op lijn ' + IntToStr(I) + #13 + E.Message+ #13+
'Text: ' + Script2.Text,
mtError, [mbIgnore, mbCancel], 0) = idCancel then Exit;
end;
end;
if I mod 50 = 0 then
PostMessage(Receiver, wm_AddProgress, Prog(N), 50);
if I = Script.Count-1 then
PostMessage(Receiver, wm_AddProgress, Prog(N), I mod 50);
end else Break;
end;
if Trim(Script2.Text) <> '' then
try
oConnection.Execute(Script2.Text, RecordsAffected, []);
except
on E: Exception do
if MessageDlg('Update error: op lijn ' + IntToStr(I) + #13 + E.Message+ #13+
'Text: ' + Script2.Text,
mtError, [mbIgnore, mbCancel], 0) = idCancel then Exit;
end;
end else PostMessage(Receiver, wm_DisableProgress, Prog(N), 0);
end;
except
{on E: Exception do
begin
PostMessage(Receiver, wm_DBCreateError, N, 0);
Ok := False;
MessageDlg(E.Message, mtWarning, [mbOk], 0);
end;}
end;
end;
finally
Script.Free;
Script2.Free;
end;
except
{on E: Exception do
begin
PostMessage(Receiver, wm_DBCreateError, 0, 0);
MessageDlg(E.Message, mtWarning, [mbOk], 0);
end;}
end;
finally
AllScripts.Free;
end;
end;
finally
oConnection.Free;
end;
if Terminated then
PostMessage(Receiver, wm_DBCreateError, 6, 0);
finally
CoUninitialize;
end;
end;
type
TYourThread = class(TThread)
private
fCallingForm: TForm;
fMemoText: string;
fIsError: boolean;
end;
procedure TYourThread.UpdMemo;
begin
if fIsEror then
fCallingForm.Memo1.Lines.Add('Error !!!');
fCallingForm.Memo1.Lines.Add(fMemoText);
end;
procedure TYourThread.AddMemo(aMemoText: string; aIsError: boolean = False);
begin
fMemoText := aMemoText;
fIsError := aIsError;
Synchronize(UpdMemo);
end;