描述:有DBGrid1、ComboBox1、ComboBox2、Edit1、btn_enter各一。DBGrid1用来显示查询结果;ComboBox1中Items的内容为“员工号”“员工名”……;ComboBox2中Items的内容为“=”“>”“<”……;Edit1为输入查找的内容;Btn_Enter为确定,其代码如下:(ADO连接SQL Server2k)procedure TF_Employee_find.btn_enterClick(Sender: TObject);
begin
with DataModuleMain.Query_Employee do
begin
close;
SQL.Clear;
SQL.Add('select * from employee');
SQL.Add('where '+ComboBox1.Items.strings[ComboBox1.ItemIndex]+
ComboBox2.Items.Strings[ComboBox2.ItemIndex]+Edit1.Text);
try
open;
except
ExecSQL;
end;
active:=true;
end;数据库中“员工号”“员工名”都为char (10)。问题:若查找输入:员工号=020,DBGrid1正常显示;
若查找输入:员工名=小明,程序报错:
Project main.exe rasied exception class EoleException with message ‘列名’ 小明’’ 无效。不知是何故?会不会是SQL SERVER2K中char空格捣得乱?
begin
with DataModuleMain.Query_Employee do
begin
close;
SQL.Clear;
SQL.Add('select * from employee');
SQL.Add('where '+ComboBox1.Items.strings[ComboBox1.ItemIndex]+
ComboBox2.Items.Strings[ComboBox2.ItemIndex]+Edit1.Text);
try
open;
except
ExecSQL;
end;
active:=true;
end;数据库中“员工号”“员工名”都为char (10)。问题:若查找输入:员工号=020,DBGrid1正常显示;
若查找输入:员工名=小明,程序报错:
Project main.exe rasied exception class EoleException with message ‘列名’ 小明’’ 无效。不知是何故?会不会是SQL SERVER2K中char空格捣得乱?
#39+ComboBox2.Items.Strings[ComboBox2.ItemIndex]+#39+Edit1.Text);
select * from table where name = 20就可以执行,因为20自动转化(整数转化为字符串)为了字符串'20',但是小明怎么自动转化呢?
所以小明必须两边加上单引号。
ComboBox2.Items.Strings[ComboBox2.ItemIndex]+QuotedStr(Edit1.Text));