我的一个表有两个字段 projectID 和 projectName,我要根据当前选择的 projectName 来查询出它对应的 projectID  :procedure TFormUseField.ComboBox1Change(Sender: TObject);
var strProject:String;
var iProject:Integer;
begin
  strProject:=combobox1.Text;
  ShowMessage(strProject);//////////////
  adoquery1.Close;
  adoQuery1.sql.Clear;
  adoquery1.SQL.Add('select projectID FROM projectInfo where ProjectName=:strProject');
  adoQuery1.open;
  iProject:=adoquery1.fieldbyname('projectID').AsInteger;
  ShowMessage(InttoStr(iProject));//////////////
end;
前一个ShowMessage的结果是对的,但后一个ShowMessage的结果总是0 , 哪里不对?

解决方案 »

  1.   

    procedure TFormUseField.ComboBox1Change(Sender: TObject);
    var strProject:String;
    var iProject:Integer;
    begin
      strProject:=combobox1.Text;
      ShowMessage(strProject);//////////////
      adoquery1.Close;
      adoQuery1.sql.Clear;
      adoquery1.SQL.Add('select projectID FROM projectInfo where ProjectName=:strProject');
      //Hear add Parameter Value 
      ADOQuery1.Parameters.ParamByName('strProject').Value := strProject;
      adoQuery1.open;
      iProject:=adoquery1.fieldbyname('projectID').AsInteger;
      ShowMessage(InttoStr(iProject));//////////////
    end;
      

  2.   

    没有给参数赋值,ADOQuery1.Parameters.ParamByName('strProject').Value := 参数值;
      

  3.   

    adoquery1.SQL.Add('select projectID FROM projectInfo where ProjectName=:strProject');
    后面加上:
    ADOQuery1.Parameters.ParamByName('strProject').Value := 'xxx';
    //其实在开始的时候,你完全可以不定义‘strProject’这个变量的,程序可以照样运行也可以这样写:
    不改动其他地方,就该sql语句就可以了:
    ---------------------------------------
    adoquery1.SQL.Add('select projectID FROM projectInfo where ProjectName='+''+strProject+'');