实现如下功能:
   用户输入用户名(edit1)和旧密码(edit2),经过验证通过后判断用户输入的两次新密码(edit3,edit4)是否一致,然后更改数据库中存的密码。但更改密码处出错,提示:'table1:Cannot perform this operation on an open dataset'.代码如下:
begin
 DataModule2.Table1.Active := true;
 with DataModule2.Table1  do
  begin
     SetKey;
     FieldByName('userID').AsString:=Edit1.Text;
     if GotoKey then
        begin
           if Locate('password',VarArrayOf([Edit2.Text]),[]) then
            begin
              if edit3.Text = edit4.Text then
                 begin
                   showmessage('ok');
                   unit2.DataModule2.Table1.Active := true;
                   unit2.DataModule2.Table1.ReadOnly := false;
                   unit2.DataModule2.Table1.Edit;
                   unit2.DataModule2.Table1.FieldByName('password').AsString:=Edit3.Text; // 设置具体数值
                   unit2.DataModule2.Table1.Post; // 写回数据库
                   unit2.DataModule2.Table1.Refresh;
                 end;
            end
           else
           showmessage('password error');
        end
     else
      showmessage('error');
  end;
end;
望高手指点,先谢过,在线等,速结贴。

解决方案 »

  1.   

    //unit2.DataModule2.Table1.ReadOnly := false;//这句话不能在数据集打开的情况下执行,应去掉
      

  2.   

    Note: Even if ReadOnly is False, users may not be able to modify or add data to a table. Other factors, such as insufficient SQL privileges for the application or its current user may prevent successful alterations.To guarantee that users cannot modify or add data to a table,1 Set the Active property to False.
    2 Set ReadOnly to True.When ReadOnly is True, the table抯 CanModify property is False.
      

  3.   

    用ADOQUERY,简单方便
    DataModule2.adoquery.clear;
    DataModule2.adoquery.open;
    DataModule2.adoquery.sql.text:=select * from table update table set 密码='''+密码.text+'''where 用户名='''+用户名+''' and 旧密码='''+bh+'''';
     data.nyq.open;