有点漏洞
改为
if uppercase(copy(trim(str.Strings[i]),1,2))='GO' then
begin
try
ExecSQL;
sql.clear;
except
begin
adoconnection1.RollbackTrans;
raise;
end;
end;
end;
procedure ExecSql(const SQLText: WideString);
var
i:integer;
str:tstringlist;
begin
str:=tstringlist.Create;
try
str.Text:=sqltext;
ADOConnection1.BeginTrans;
with adoquery1 do
begin
close;
sql.Clear;
for i:=0 to str.Count-1 do
begin
if ((copy(trim(str.Strings[i]),1,2)<>'/*') and
(copy(trim(str.Strings[i]),1,2)<>'--') and
(uppercase(copy(trim(str.Strings[i]),1,2))<>'GO')) then
SQL.Add(Str.Strings[i]);
if uppercase(copy(trim(str.Strings[i]),1,2))='GO' then
begin
try
ExecSQL;
except
begin
adoconnection1.RollbackTrans;
raise;
end;
end;
end;
end;
end;
ADOConnection1.CommitTrans;
finally
str.Free;
end;
end;
调用示例
procedure tform1.button1click(Sender: TObject);
var
sqltext:tstringlist;
begin
sqltext:=tstringlist.Create;
try
sqltext.Add('delete from Comm_LinkMan where Genre_ID='+LinkMan_Genre.fieldbyname('Genre_ID').AsString );
sqltext.Add('go');
sqltext.Add('delete from Comm_LinkMan_Genre where Genre_ID='+LinkMan_Genre.fieldbyname('Genre_ID').AsString );
sqltext.Add('go');
ClientDMForm.appserver.ExecSql(sqltext.Text);
LinkMan_Genre.Refresh;
LinkMan.Refresh;
CreateTree;
finally
sqltext.Free;
end;
end;
通过Tstringlist读取文件
mystrings :Tstringlist;
Mystrings :=Tstringlist.create;
Mystrings.loadfromfile(filename)
filename可以通过opendialog来获取
for i:=0 to mystrings.count-1 do
if lowercase(mystrings[i])='go' then