procedure TForm1.Button7Click(Sender: TObject); var i:integer; begin DM1.ADOConnection1.Connected:=False; DM1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password=nohacker;Persist Security Info=True;User ID=sa;Initial Catalog=cjtj;Data Source=192.168.235.113'; with DM1.ADOQuery1 do begin Close; SQL.Clear; SQL.Add('select * from teacherinfo;'); ExecSQL; showmessage('ok1'); try DM1.ADOConnection1.BeginTrans; for i:=1 to 10 do begin Close; SQL.Clear; SQL.Add('insert into teacherinfo(teachername,subject)'); SQL.Add('values(:name,:sub);'); Parameters.ParamByName('name').Value:=inttostr(i); Parameters.ParamByName('sub').Value:=inttostr(i); ExecSQL;//第一次执行到这就异常,回滚了 end; showmessage('good,begin'); if MessageDlg('yes or no?',mtInformation,[mbYes,mbNo],0)=mrYes then DM1.ADOConnection1.CommitTrans else Dm1.ADOConnection1.RollbackTrans; except showmessage('shit'); DM1.ADOConnection1.RollbackTrans; end; end; end;发生错误的地方我作了说明了 我想知道,如果是用事务,那我在事务中进行的多次对数据库的操作,是不是都是 在本地缓存中进行的呢??
var
i:integer;
begin
DM1.ADOConnection1.Connected:=False;
DM1.ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password=nohacker;Persist Security Info=True;User ID=sa;Initial Catalog=cjtj;Data Source=192.168.235.113';
with DM1.ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('select * from teacherinfo;');
ExecSQL;
showmessage('ok1');
try
DM1.ADOConnection1.BeginTrans;
for i:=1 to 10 do
begin
Close;
SQL.Clear;
SQL.Add('insert into teacherinfo(teachername,subject)');
SQL.Add('values(:name,:sub);');
Parameters.ParamByName('name').Value:=inttostr(i);
Parameters.ParamByName('sub').Value:=inttostr(i);
ExecSQL;//第一次执行到这就异常,回滚了
end;
showmessage('good,begin');
if MessageDlg('yes or no?',mtInformation,[mbYes,mbNo],0)=mrYes then
DM1.ADOConnection1.CommitTrans
else
Dm1.ADOConnection1.RollbackTrans;
except
showmessage('shit');
DM1.ADOConnection1.RollbackTrans;
end;
end;
end;发生错误的地方我作了说明了
我想知道,如果是用事务,那我在事务中进行的多次对数据库的操作,是不是都是
在本地缓存中进行的呢??
有异常当然要回滚了!应该是插入错误,检查SQL
帮忙
要知道,你如果使用SQL语句进行数据的更新,SQLsever会自动启用事务处理
所以,要实现你所说的目的
你可以使用事务处理+缓存更新