我想用ADOQUERY控件往SQL数据库中添加记录,但不知为何运行时老提示出错.请各位帮忙.
    adoquery1.Close;
    adoquery1.SQL.Clear;
    adoquery1.SQL.add('insert into thing1(number,name,department,ip)');
    adoquery1.SQL.Add('values (:number,:name,:department,:ip)');
    adoquery1.Parameters.ParamByName('number').Value := inttostr(i);
    adoquery1.Parameters.ParamByName('name').Value := combobox1.Text;
    adoquery1.Parameters.ParamByName('department').Value :=combobox2.Text;
    adoquery1.Parameters.ParamByName('ip').Value := combobox3.Text;
    try
    adoquery1.Open;
    except
    adoquery1.ExecSQL;
    end;

解决方案 »

  1.   

    try
    adoquery1.ExecSQL;
    except
    showmessage('error');   
    end;
    个人感觉用adoquery1.append比写insert好一些!
      

  2.   

    楼上是对的,不过你的也没有错;建议直接用DBEdit、DBMemo、DBComBoBox等来操作,或用ADOQuery1.FieldByName('sName').AsString := Edit1.Text更是方法而少用直接操作SQL语句等方法,再前用ADOQuery1.Append、其后用ADOQuery1.Post就可以实现
    对于你的代码,name用[name]代替实现试试;还有,输入的数据是否有:出现ip)'->ip) ',留个空格再看看
      

  3.   

    直接操作Sql语句有时候更灵活。还有提问题是最后把报的什么错写出来,因为根据抱的这个错误才更容易找到问题。比如说我告诉你“我的电脑坏了,你知道什么原因吗?”你能告诉我答案吗?
      

  4.   

    有几点需要注意:
    执行insert等操作时只能用ExecSQL而不能用open ,
    你的几个字段名可能和关键字有处突,在写sql的时候把每个字段用[]括起来。adoquery1.SQL.add('insert into thing1(number,name,department,ip)');
        adoquery1.SQL.Add('values (:number,:name,:department,:ip)');
    这两句之间没有空格,应该在values前加空格
      

  5.   

    除了查詢語句外,其它的都用ExecSQL
      

  6.   

    adoquery1.Close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.add('insert into thing1(number,name,department,ip)');
        adoquery1.SQL.Add(' values (:number,:name,:department,:ip)');
        adoquery1.Parameters.ParamByName('number').Value := inttostr(i);
        adoquery1.Parameters.ParamByName('name').Value := combobox1.Text;
        adoquery1.Parameters.ParamByName('department').Value :=combobox2.Text;
        adoquery1.Parameters.ParamByName('ip').Value := combobox3.Text;
        adoquery1.ExecSQL;
        end;空格
    直接用EexecSQL
      

  7.   

    你把添加的地方这样写ADOQuery.Insert;
    ADOQuery1.FieldValues['字段名']:=值;
    ..
    ..
    ADOQuery1.Post;
    再看看是不是修改了关键字
      

  8.   

    除了Select語句外,其它的都用ExecSQL
    你直接运行exe应该就不报错了!