我在练习编写两条件“模糊查询”过程中,遇到了数据类型转换的问题,编写出的程序,通过字符型字段查询,没有出错,而通过数字型(比如数据库中某表的“数学成绩”字段)查询时,就出现了数据类型不匹配的问题,我也试图添加类型转换函数,但是编译通不过,恳请您的帮助!部分代码如下:
procedure TForm1.Button1Click(Sender: TObject);var
  val1,val2,fld1,fld2,opt1,opt2:string;
  andor:string;
  queryconnstr:string;begin
  adotable1.Active:=false;  queryconnstr:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=STUDENTS1.mdb';
  adoquery1.ConnectionString:=queryconnstr;
  adoquery1.Active:=false;  if radiobutton1.Checked=true  then
    andor:='and'
  else
    andor:='or';
  val1:=trim(edit1.Text);
  val2:=trim(edit2.Text);
  opt1:=trim(combobox2.Text);
  opt2:=trim(combobox4.Text);
  fld1:=trim(combobox1.Text);
  fld2:=trim(combobox3.text);  adoquery1.Close;
  adoquery1.SQL.Clear;
  adoquery1.sql.Add('select * from 成绩单 ');  if  trim(edit1.Text)<>'' then
    begin
      if(opt1<>'包含') then
        //if (fld1<>'学号') and (fld1<>'姓名') then//注释部分是我试图加进去的代码但不成功
          //adoquery1.SQL.add('where ('+fld1+opt1+''''+StrToInt(val1)+''''+')')
          //adoquery1.SQL.add('where ('+fld1+opt1+StrToInt(val1)+')')
        //else
          adoquery1.SQL.add('where ('+fld1+opt1+''''+val1+''''+')')
      else
        //if (fld1<>'学号') and (fld1<>'姓名') then
          //adoquery1.SQL.add('where ('+fld1+opt1+''''+StrToInt(val1)+''''+')')
        //else
          adoquery1.SQL.Add('where('+fld1+' like'+''''+'%'+val1+'%'+''''+')');      if(opt2<>'包含') then
        adoquery1.SQL.Add(andor+'('+fld2+opt2+''''+val2+''''+')' )
      else
        adoquery1.SQL.Add(andor+'('+fld2+' like'+''''+'%'+val2+'%'+''''+')');
   end;  adoquery1.open;
  datasource1.DataSet:=adoquery1;
  adoquery1.Active:=true;
  dbgrid1.DataSource:=datasource1;end;

解决方案 »

  1.   

    对于这种制造SQL语句的问题,建议楼主单步调试看最后的SQL语句是什么样子一般都可以发现问题的所在对于楼主所说的那部分注释加不进去,是不是由于在if语句后面有两个语句而楼主没有用begin...end括起来?
      

  2.   

    1. 首先我对你提出一个小小的批评:你的代码风格有待调整,不要问题大小,一定要养成好的编码风格.
    2.对于类型转换的问题,我想是不是可以采取另外一个方法:利用adoquery1的参数赋值,转换过程教由adoquery1,只是程序处理到变体类型,性能不是很好(但是一般应用下看不出来)
      

  3.   

    类型转换的问题,在SQL里转换也行.用CONVERT或CAST来转换.
      

  4.   

    两种写法
    第一种
    var
      i :integer;
    begin
      with adoquery1 do
      begin
        close;
        sql.clear;
        sql.add('select * from 数字字段 = :iParam');
        parameters.parambyname('iParam').value := i;
        open;
      end;
    end;  
    第二种
    var
      i :integer;
    begin
      with adoquery1 do
      begin
        close;
        sql.clear;
        sql.add(Format('select * from 数字字段 = %d',[i]));
        open;
      end;
    end;
      

  5.   

    谢谢楼上各位的指导,我修改了一下,现在只能实现第一组模糊查询条件的检索,第二组用第一组的方法如法炮制,好象不行,请指点!谢谢procedure TForm1.Button1Click(Sender: TObject);var
      val1,val2,fld1,fld2,opt1,opt2:string;
      andor:string;
      queryconnstr,sqlstr:string;
      i:integer;begin
      adotable1.Active:=false;  queryconnstr:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=STUDENTS1.mdb';
      adoquery1.ConnectionString:=queryconnstr;  if radiobutton1.Checked=true  then
        andor:='and'
      else
        andor:='or';  val1:=trim(edit1.Text);
      val2:=trim(edit2.Text);
      opt1:=trim(combobox2.Text);
      opt2:=trim(combobox4.Text);
      fld1:=trim(combobox1.Text);
      fld2:=trim(combobox3.text);
      i:=StrToInt(val1);
      adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.sql.Add('select * from 成绩单 ');  if  trim(edit1.Text)<>'' then
        begin
          if(opt1<>'包含') then
            if (fld1<>'学号') and (fld1<>'姓名') then
              begin
                adoquery1.SQL.Clear;
                adoquery1.sql.add('select * from 成绩单 where ('+fld1+''+opt1+':iParam)');
                adoquery1.parameters.parambyname('iParam').value := i;
              end
            else
              begin
                adoquery1.SQL.add('where ('+fld1+opt1+''''+val1+''''+')')
              end
          else
            begin
              adoquery1.SQL.Add('where('+fld1+' like'+''''+'%'+val1+'%'+''''+')');
            end;
          if(opt2<>'包含') then
            {if (fld2<>'学号') and (fld2<>'姓名') then
              begin
                adoquery1.SQL.Add(andor+'('+fld2+opt2+':value2'+')');
                adoquery1.parameters.parambyname('value2').value := j;
              end
            else}
              begin
                adoquery1.SQL.Add(andor+'('+fld2+opt2+''''+val2+''''+')' )
              end
          else
            begin
              adoquery1.SQL.Add(andor+'('+fld2+' like'+''''+'%'+val2+'%'+''''+')');
            end;
       end;  adoquery1.open;
      datasource1.DataSet:=adoquery1;
      adoquery1.Active:=true;
      dbgrid1.DataSource:=datasource1;end;