我的保存代码:
with DataModule1 do
  begin
    if not Database1.Connected then
      exit;
    if saveButton_choice=1 then     //选择保存第一张表
     begin
       with table1 do
       begin
         Database1.StartTransaction;
         try
           ApplyUpdates;
           Database1.Commit;
         except
          application.MessageBox('不能重复输入,请重新输入再保存!','确认',MB_OK);
          Database1.Rollback;
          raise;
         end;
         commitupdates;
       end;
     end;     //以上可以执行保存第一张表!
    if saveButton_choice=2 then       //选择保存第二张表
      begin
        with table2 do
          begin
          Database1.StartTransaction;
           try
           ApplyUpdates;                 //这里出错!!!
           Database1.Commit;
           except
           application.MessageBox('不能重复输入,请重新输入再保存!','确认',MB_OK);
           Database1.Rollback;
           raise;
           end;
           commitupdates;
        end;
      end;
  end;
为什么两部分代码一样,却不能进行同样的操作???

解决方案 »

  1.   

    因为是断码片段,不太好说,
    给两个建议
    1、把代码分离开,分为两个按钮下的事件处理代码
    2、将所有的 WITH 去掉
      

  2.   

    想请教 去掉with 有什么意义 ?~    还有就是我的另一个问题,为什么在dbgrid里输入单元格或删除行会出现重复行?我查代码发现dataset的updatedata执行了两次,单是不知道怎么解决,高手帮帮我!