H
heping
Unregistered / Unconfirmed
GUEST, unregistred user!
在作ADO数据更新时,由于,我同时更新两张表。所以建了个事务。
但。操作后却是两张表都没有被更新。我去掉事务。单独更新两张表。能正确运行。
请问是何故?
try //写数据到'原始单'和'总库库存'
with DataModule1 do
begin
ADOConnection1.BeginTrans; //开始一个事务
with AdoQuery_zbys_bsc do //写原始单
begin
Close;
Sql.Clear;
Sql.Add('update yszb set ysrq=:ysrq1,hpmc=:hpmc1,ydjh=:ydjh1,hpybh=:hpybh1,sccs=:sccs1,kh=:kh1,dw=:dw1,sc=:sc1,');
Sql.Add('jjz=:jjz1,zz=:zz1,lsj=:lsj1,js=:js1,lsje=:lsje1');
Sql.Add(' where (ysdh=:ysdh1) and (hpbh=:hpbh1)');
Parameters[0].Value := Now;
Parameters[1].Value := Trim(Edit_hpmc.Text);
Parameters[2].Value := Trim(Edit_ydjh.Text);
Parameters[3].Value := Trim(Edit_hpybh.Text);
Parameters[4].Value := Trim(ComboBox_sccs.Text);
Parameters[5].Value := Trim(Edit_kh.Text);
Parameters[6].Value := Trim(ComboBox_dw.Text);
Parameters[7].Value := StrToFloat(Trim(Edit_sc.Text));
Parameters[8].Value := StrToFloat(Trim(Edit_jjz.Text));
Parameters[9].Value := StrToFloat(Trim(Edit_zz.Text));
Parameters[10].Value := StrToFloat(Trim(Edit_lsj.Text));
Parameters[11].Value := StrToInt(Trim(Edit_js.Text));
Parameters[12].Value := StrToFloat(Trim(Edit_lsje.Text));
Parameters[13].Value := Trim(Label_ysdh.Caption);
Parameters[14].Value := Trim(Edit_hpbh.Text);
Prepared := True;
Execsql;
end;
//写库存表
with AdoQuery_zbys_bsc2 do
begin
Close;
Sql.Clear;
Sql.Add('update kcbsczb set rkrq=:rkrq1,hpmc=:hpmc1,hpybh=:hpybh1,sccs=:sccs1,kh=:kh1,dw=:dw1,sc=:sc1,');
Sql.Add('jjz=:jjz1,zz=:zz1,lsj=:lsj1,js=:js1,lsje=:lsje1');
Sql.Add(' where hpbh=:hpbh1');
Parameters[0].Value := Now;
Parameters[1].Value := Trim(Edit_hpmc.Text);
Parameters[2].Value := Trim(Edit_hpybh.Text);
Parameters[3].Value := Trim(ComboBox_sccs.Text);
Parameters[4].Value := Trim(Edit_kh.Text);
Parameters[5].Value := Trim(ComboBox_dw.Text);
Parameters[6].Value := StrToFloat(Trim(Edit_sc.Text));
Parameters[7].Value := StrToFloat(Trim(Edit_jjz.Text));
Parameters[8].Value := StrToFloat(Trim(Edit_zz.Text));
Parameters[9].Value := StrToFloat(Trim(Edit_lsj.Text));
Parameters[10].Value := StrToInt(Trim(Edit_js.Text));
Parameters[11].Value := StrToFloat(Trim(Edit_lsje.Text));
Parameters[12].Value := Trim(Edit_hpbh.Text);
Prepared := True;
Execsql;
end;
ADOConnection1.CommitTrans; //提交
end;
except
on EDatabaseError do
begin
Application.MessageBox('数据库出错', '错误', MB_OK + MB_ICONERROR);
if DataModule1.ADOConnection1.InTransaction = True then
begin
DataModule1.ADOConnection1.RollbackTrans;
end;
Exit;
end;
else
begin
Application.MessageBox('系统出错', '错误', MB_OK + MB_ICONERROR);
if DataModule1.ADOConnection1.InTransaction = True then
begin
DataModule1.ADOConnection1.RollbackTrans;
end;
Exit;
end;
end;
但。操作后却是两张表都没有被更新。我去掉事务。单独更新两张表。能正确运行。
请问是何故?
try //写数据到'原始单'和'总库库存'
with DataModule1 do
begin
ADOConnection1.BeginTrans; //开始一个事务
with AdoQuery_zbys_bsc do //写原始单
begin
Close;
Sql.Clear;
Sql.Add('update yszb set ysrq=:ysrq1,hpmc=:hpmc1,ydjh=:ydjh1,hpybh=:hpybh1,sccs=:sccs1,kh=:kh1,dw=:dw1,sc=:sc1,');
Sql.Add('jjz=:jjz1,zz=:zz1,lsj=:lsj1,js=:js1,lsje=:lsje1');
Sql.Add(' where (ysdh=:ysdh1) and (hpbh=:hpbh1)');
Parameters[0].Value := Now;
Parameters[1].Value := Trim(Edit_hpmc.Text);
Parameters[2].Value := Trim(Edit_ydjh.Text);
Parameters[3].Value := Trim(Edit_hpybh.Text);
Parameters[4].Value := Trim(ComboBox_sccs.Text);
Parameters[5].Value := Trim(Edit_kh.Text);
Parameters[6].Value := Trim(ComboBox_dw.Text);
Parameters[7].Value := StrToFloat(Trim(Edit_sc.Text));
Parameters[8].Value := StrToFloat(Trim(Edit_jjz.Text));
Parameters[9].Value := StrToFloat(Trim(Edit_zz.Text));
Parameters[10].Value := StrToFloat(Trim(Edit_lsj.Text));
Parameters[11].Value := StrToInt(Trim(Edit_js.Text));
Parameters[12].Value := StrToFloat(Trim(Edit_lsje.Text));
Parameters[13].Value := Trim(Label_ysdh.Caption);
Parameters[14].Value := Trim(Edit_hpbh.Text);
Prepared := True;
Execsql;
end;
//写库存表
with AdoQuery_zbys_bsc2 do
begin
Close;
Sql.Clear;
Sql.Add('update kcbsczb set rkrq=:rkrq1,hpmc=:hpmc1,hpybh=:hpybh1,sccs=:sccs1,kh=:kh1,dw=:dw1,sc=:sc1,');
Sql.Add('jjz=:jjz1,zz=:zz1,lsj=:lsj1,js=:js1,lsje=:lsje1');
Sql.Add(' where hpbh=:hpbh1');
Parameters[0].Value := Now;
Parameters[1].Value := Trim(Edit_hpmc.Text);
Parameters[2].Value := Trim(Edit_hpybh.Text);
Parameters[3].Value := Trim(ComboBox_sccs.Text);
Parameters[4].Value := Trim(Edit_kh.Text);
Parameters[5].Value := Trim(ComboBox_dw.Text);
Parameters[6].Value := StrToFloat(Trim(Edit_sc.Text));
Parameters[7].Value := StrToFloat(Trim(Edit_jjz.Text));
Parameters[8].Value := StrToFloat(Trim(Edit_zz.Text));
Parameters[9].Value := StrToFloat(Trim(Edit_lsj.Text));
Parameters[10].Value := StrToInt(Trim(Edit_js.Text));
Parameters[11].Value := StrToFloat(Trim(Edit_lsje.Text));
Parameters[12].Value := Trim(Edit_hpbh.Text);
Prepared := True;
Execsql;
end;
ADOConnection1.CommitTrans; //提交
end;
except
on EDatabaseError do
begin
Application.MessageBox('数据库出错', '错误', MB_OK + MB_ICONERROR);
if DataModule1.ADOConnection1.InTransaction = True then
begin
DataModule1.ADOConnection1.RollbackTrans;
end;
Exit;
end;
else
begin
Application.MessageBox('系统出错', '错误', MB_OK + MB_ICONERROR);
if DataModule1.ADOConnection1.InTransaction = True then
begin
DataModule1.ADOConnection1.RollbackTrans;
end;
Exit;
end;
end;