function TFareFram.CheckData : Boolean; begin result:=false; if DMFreights.Queryfreight.Connection.InTransaction then DMFreights.Queryfreight.Connection.RollbackTrans; DMFreights.Queryfreight.Connection.BeginTrans;// /开始事务 try // Connection.BeginTrans;// /开始事务 if DMFreights.Queryfreight.RecordCount>0 then begin with DMFreights.Queryfreight do begin if VarIsNull(FieldValues['Payer']) or VarIsEmpty(FieldValues['Payer'])or VarIsNull(FieldValues['Account'])or VarIsEmpty(FieldValues['Account']) or VarIsNull(FieldValues['freight_name'])or VarIsEmpty(FieldValues['freight_name']) or VarIsNull(FieldValues['Remained'])or VarIsEmpty(FieldValues['Remained']) then begin result:=false; slkDBGrid1.SetFocus; DMFreights.Queryfreight.Connection.RollbackTrans; exit; end else begin if DMFreights.Queryfreight.FieldByName('Account').AsFloat = 0 then begin result:=false; slkDBGrid2.SetFocus; DMFreights.Queryfreight.Connection.RollbackTrans; exit; end else begin UpdateBatch; Refresh end; end; end; end else begin with DMFreights.Queryfreight do begin UpdateBatch; refresh; end; end; if Panel1.Visible= true then begin if DMFreights.Queryfreight1.RecordCount>0 then begin with DMFreights.Queryfreight1 do begin if VarIsNull(FieldValues['Payer'])or VarIsEmpty(FieldValues['Payer']) or VarIsNull(FieldValues['Account'])or VarIsEmpty(FieldValues['Account']) or VarIsNull(FieldValues['freight_name'])or VarIsEmpty(FieldValues['freight_name']) or VarIsNull(FieldValues['Remained'])or VarIsEmpty(FieldValues['Remained']) then begin result:=false; slkDBGrid2.SetFocus; DMFreights.Queryfreight.Connection.RollbackTrans; exit; end else begin if DMFreights.Queryfreight1.FieldByName('Account').AsFloat = 0 then begin result:=false; slkDBGrid2.SetFocus; DMFreights.Queryfreight.Connection.RollbackTrans; exit; end else begin UpdateBatch; Refresh end; end; end; end else begin with DMFreights.Queryfreight1 do begin UpdateBatch; refresh; end; end; end; DMFreights.Queryfreight.Connection.CommitTrans; except DMFreights.Queryfreight.Connection.RollbackTrans; exit; end; result:=true; end;
上面的代码太多了,是不是有必要这么多代码呢。 我使用的是ado连接到sql 20000上的。 有的说使用事务处理如下: if (not adoconnection1.intransaction) then begin adoconnection1.begintrans; end ; try //你的代码 adoconnection1.committrans; except adoconnection1.rollbacktrans; end;但不知道上面的这些语句是放在哪个控件的哪个属性中?
放在adoquery中的beforepost事件里吧
不需要事务处理,事物只是为了保证数据的一致性 ; Sql Server 2000 在更新表时会自动锁表,也就是每个时间单位最多只有一个用户更新表
begin
result:=false;
if DMFreights.Queryfreight.Connection.InTransaction then
DMFreights.Queryfreight.Connection.RollbackTrans;
DMFreights.Queryfreight.Connection.BeginTrans;// /开始事务 try
// Connection.BeginTrans;// /开始事务
if DMFreights.Queryfreight.RecordCount>0 then
begin
with DMFreights.Queryfreight do
begin
if VarIsNull(FieldValues['Payer']) or VarIsEmpty(FieldValues['Payer'])or VarIsNull(FieldValues['Account'])or VarIsEmpty(FieldValues['Account']) or VarIsNull(FieldValues['freight_name'])or VarIsEmpty(FieldValues['freight_name']) or
VarIsNull(FieldValues['Remained'])or VarIsEmpty(FieldValues['Remained']) then
begin
result:=false;
slkDBGrid1.SetFocus;
DMFreights.Queryfreight.Connection.RollbackTrans;
exit;
end
else
begin
if DMFreights.Queryfreight.FieldByName('Account').AsFloat = 0 then
begin
result:=false;
slkDBGrid2.SetFocus;
DMFreights.Queryfreight.Connection.RollbackTrans;
exit;
end
else
begin
UpdateBatch;
Refresh
end;
end;
end;
end
else
begin
with DMFreights.Queryfreight do
begin
UpdateBatch;
refresh;
end;
end; if Panel1.Visible= true then
begin
if DMFreights.Queryfreight1.RecordCount>0 then
begin
with DMFreights.Queryfreight1 do
begin
if VarIsNull(FieldValues['Payer'])or VarIsEmpty(FieldValues['Payer']) or VarIsNull(FieldValues['Account'])or VarIsEmpty(FieldValues['Account']) or VarIsNull(FieldValues['freight_name'])or VarIsEmpty(FieldValues['freight_name']) or
VarIsNull(FieldValues['Remained'])or VarIsEmpty(FieldValues['Remained']) then
begin
result:=false;
slkDBGrid2.SetFocus;
DMFreights.Queryfreight.Connection.RollbackTrans;
exit;
end
else
begin
if DMFreights.Queryfreight1.FieldByName('Account').AsFloat = 0 then
begin
result:=false;
slkDBGrid2.SetFocus;
DMFreights.Queryfreight.Connection.RollbackTrans;
exit;
end
else
begin
UpdateBatch;
Refresh
end;
end;
end;
end
else
begin
with DMFreights.Queryfreight1 do
begin
UpdateBatch;
refresh;
end;
end;
end;
DMFreights.Queryfreight.Connection.CommitTrans;
except
DMFreights.Queryfreight.Connection.RollbackTrans;
exit;
end;
result:=true;
end;
我使用的是ado连接到sql 20000上的。
有的说使用事务处理如下: if (not adoconnection1.intransaction) then
begin
adoconnection1.begintrans;
end ;
try
//你的代码
adoconnection1.committrans;
except
adoconnection1.rollbacktrans;
end;但不知道上面的这些语句是放在哪个控件的哪个属性中?
Sql Server 2000 在更新表时会自动锁表,也就是每个时间单位最多只有一个用户更新表
我发现是第1位修改的数据被系统接受,而其它用户再修改时,虽然不接受,但也没有什么提示,这样总觉得不怎么方便。
作用是当try块中的代码在执行过程中发生错误(也就是异常),则执行except块中的内容,而发生异常后的语句块不在执行,直接执行end后的语句。目的是要增加代码的健壮性和容错性,使程序发生异常时不至于退出。