小弟写了一段代码,如下:
procedure Tuserinfofrm.BitBtn2Click(Sender: TObject);
begin
if RadioButton1.Checked  then
  begin
   If not checkmodified(Edit1.Text,Edit2.Text,usertype.ItemIndex) then exit;
    Quser.SQL.Add('update user_info set 学生证号=:str_no,等级=:class where 姓名=:name');
    Quser.ParamByName('name').AsString:=Edit1.Text;
    Quser.ParamByName('str_no').AsString:=Edit2.Text;
    Quser.ParamByName('class').AsString:=inttostr(usertype.ItemIndex);
    Quser.ExecSQL;
    Messagedlg('修改成功',mtinformation,[mbYes],0);
  end;
if RadioButton2.Checked then
  begin
    if trim(Edit1.Text)='' then
    begin
      showmessage('用户名不能为空!');
      exit;
    end;
    if checkuserexit(Edit1.Text) then
    begin
      showmessage('该用户已经存在,请重新输入用户名!');
      exit;
    end;
    Quser.SQL.Add('insert into user_info(姓名,学生证号,等级) values(:name,:str_no,class)');
    Quser.ParamByName('name').AsString:=Edit1.Text;
    Quser.ParamByName('str_no').AsString:=Edit2.Text;
    Quser.ParamByName('class').AsString:=inttostr(usertype.ItemIndex);
    showmessage('1');
    Quser.ExecSQL;
    Quser.Post;
    Messagedlg('添加用户成功!',mtinformation,[mbYes],0);
  end;
  listuserinfo;
  Quser.Close;
  Quser.Free;
end;
但在运行时老出现“probject probject1.exe raised exception class EAccessViolation wity message 'Access violation at address 004B2220 in module'project1.ext'.read of address 00000248'.process stopped.use step or run to contion”.错误提示,不知是什么原因,请各位大侠们帮帮忙!!!谢谢了!!!
上面用到的函数没有问题,错误提示就出在insert 语句和update语句上。

解决方案 »

  1.   

    Quser.close;
    Quser.SQL.clear;//加上这两句
    Quser.SQL.add........
      

  2.   

    procedure Tuserinfofrm.BitBtn2Click(Sender: TObject);
    begin
    if RadioButton1.Checked  then
      begin
       If not checkmodified(Edit1.Text,Edit2.Text,usertype.ItemIndex) then exit;
        Quser.close;
        Quser.SQL.clear;//加上这两句
        Quser.SQL.Add('update user_info set 学生证号=:str_no,等级=:class where 姓名=:name');
        Quser.ParamByName('name').AsString:=Edit1.Text;
        Quser.ParamByName('str_no').AsString:=Edit2.Text;
        Quser.ParamByName('class').AsString:=inttostr(usertype.ItemIndex);
        Quser.ExecSQL;
        Messagedlg('修改成功',mtinformation,[mbYes],0);
      end;
    if RadioButton2.Checked then
      begin
        if trim(Edit1.Text)='' then
        begin
          showmessage('用户名不能为空!');
          exit;
        end;
        if checkuserexit(Edit1.Text) then
        begin
          showmessage('该用户已经存在,请重新输入用户名!');
          exit;
        end;
        Quser.close;
        Quser.SQL.clear;//加上这两句
        Quser.SQL.Add('insert into user_info(姓名,学生证号,等级) values(:name,:str_no,class)');
        Quser.ParamByName('name').AsString:=Edit1.Text;
        Quser.ParamByName('str_no').AsString:=Edit2.Text;
        Quser.ParamByName('class').AsString:=inttostr(usertype.ItemIndex);
        showmessage('1');
        Quser.ExecSQL;
        //Quser.Post;去掉这一句
        Messagedlg('添加用户成功!',mtinformation,[mbYes],0);
      end;
      listuserinfo;
      //Quser.Close;
      //Quser.Free;如果你的Quser不是动态生成的,这两句也应该去掉
    end;