我在练习编写两条件“模糊查询”过程中,遇到了数据类型转换的问题,编写出的程序,通过字符型字段查询,没有出错,而通过数字型(比如数据库中某表的“数学成绩”字段)查询时,就出现了数据类型不匹配的问题,我也试图添加类型转换函数,但是编译通不过,恳请您的帮助!部分代码如下:
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;
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;
2.对于类型转换的问题,我想是不是可以采取另外一个方法:利用adoquery1的参数赋值,转换过程教由adoquery1,只是程序处理到变体类型,性能不是很好(但是一般应用下看不出来)
第一种
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;
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;