当前位置:编程学习 > Delphi >>

Delphi中利用ADOQuery向数据库中添加数据错误

procedure Tperson_form.Button2Click(Sender: TObject);
begin
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('insert into student');
adoquery2.SQL.Add('Values(:no,:name,:sex,:class,:dom,:tel,:home,:htel)');
adoquery2.Parameters[0].Value:=no.Text;
adoquery2.Parameters[1].Value:=name.Text;
adoquery2.Parameters[2].Value:=sex.Text;
adoquery2.Parameters[3].Value:=Strtoint(grade.Text);
adoquery2.Parameters[4].Value:=dom.Text;
adoquery2.Parameters[5].Value:=phone.Text;
adoquery2.Parameters[6].Value:=addr.Text;
adoquery2.Parameters[7].Value:=htel.Text;
adoquery2.ExecSQL;
showmessage('新书添加成功');
end;

执行以上程序段弹出错误list index out of bounds

为什么啊?

追问:改完后还是有问题,提示为ADOquery2:parameter'no'not found
答案:提示数组越界了,建议你将下面的代码
adoquery2.Parameters[0].Value:=no.Text;
adoquery2.Parameters[1].Value:=name.Text;
adoquery2.Parameters[2].Value:=sex.Text;
adoquery2.Parameters[3].Value:=Strtoint(grade.Text);
adoquery2.Parameters[4].Value:=dom.Text;
adoquery2.Parameters[5].Value:=phone.Text;
adoquery2.Parameters[6].Value:=addr.Text;
adoquery2.Parameters[7].Value:=htel.Text;
改成:
adoquery2.Parameters.ParamByName('no').Value:=no.Text;
adoquery2.Parameters.ParamByName('name').Value:=name.Text;
adoquery2.Parameters.ParamByName('sex').Value:=sex.Text;
adoquery2.Parameters.ParamByName('class').Value:=Strtoint(grade.Text);
adoquery2.Parameters.ParamByName('dom').Value:=dom.Text;
adoquery2.Parameters.ParamByName('tel').Value:=addr.Text;
adoquery2.Parameters.ParamByName('home').Value:=phone.Text;
adoquery2.Parameters.ParamByName('htel').Value:=htel.Text;
这样可以避免错误,而且就算出错也可以很快的定位错误的位置

上一个:关于Pascal和Delphi两种语言的区别和介绍
下一个:delphi如何实现添加、删除、更新、检索

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,