只处理了一个参数值,两个参数值的你加一下
//单元文件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Edit1: TEdit;
Edit2: TEdit;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var StrTemp1:string;
StrTemp2:string;
fieldList:TStringList;
StrSQL,sWhere:string;
i:integer;
begin
adoconnection1.Close ;
adoconnection1.ConnectionString :='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=mytest;Data Source=host;';
adoconnection1.Open ;
StrTemp1:=Edit1.Text;
StrTemp2:=Edit2.Text;
//IntTemp1:=StrToInt(Edit1.Text);
//IntTemp2:=StrToInt(Edit2.Text)
// 这里不要用别名
StrSQL:='select s.sno ,sname ,ssex ,sage ,sdept ,sc.cno ,'+
'grade ,cname ,ccredit ,semster '+
'from student s left join sc on s.sno=sc.sno left join course c on sc.cno=c.cno '+
'where 1=2';//空集
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.text:=StrSQL;
adoquery1.open
;
StrSQL:='select s.sno 学号,sname 姓名,ssex 性别,sage 年龄,sdept 所在系,sc.cno 课程号,'+
'grade 分数,cname 课程名,ccredit 学分,semster 学期 '+
'from student s left join sc on s.sno=sc.sno left join course c on sc.cno=c.cno ';
fieldList:=TStringList.Create ;
for i:=0 to adoquery1.FieldCount -1 do
begin
if not(adoquery1.Fields.DataType in [ftSmallint,ftInteger,ftWord,ftFloat,ftCurrency ]) then
fieldList.Add(adoquery1.Fields.FieldName );
end;
adoquery1.Close ;
try
StrToInt(Edit1.Text);
sWhere:='(s.sno='''+StrTemp1+''' or sname='''+StrTemp1+''' or ssex='''+StrTemp1+''' or sage='''+
StrTemp1+''' or sdept='''+StrTemp1+''' or sc.cno='''+StrTemp1+''' or grade='''+StrTemp1+
''' or cname='''+StrTemp1+''' or ccredit='''+StrTemp1+''' or semster='''+StrTemp1+''')';
except
sWhere:='';
for i:=0 to fieldlist.count-1 do
begin
if fieldlist='sno' then
//指定列的所有者,不加不行
sWhere:=sWhere+' or s.'+ fieldlist+'='''+StrTemp1+''''
else
if fieldlist='cno' then
sWhere:=sWhere+' or sc.'+ fieldlist+'='''+StrTemp1+''''
else sWhere:=sWhere+' or '+ fieldlist+'='''+StrTemp1+'''';
end;
delete (sWhere ,1 ,3);
end;
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.text:=StrSQL+' where '+sWhere ;
showmessage(StrSQL+' '+sWhere);
adoquery1.open;
FreeAndNil(fieldList) ;
end;
end.
//form文件
object Form1: TForm1
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Edit1: TEdit
Left = 72
Top = 48
Width = 121
Height = 21
TabOrder = 0
Text = 'Edit1'
end
object Edit2: TEdit
Left = 216
Top = 48
Width = 121
Height = 21
TabOrder = 1
Text = 'Edit2'
end
object DBGrid1: TDBGrid
Left = 16
Top = 88
Width = 505
Height = 249
DataSource = DataSource1
TabOrder = 2
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
end
object Button1: TButton
Left = 352
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 3
OnClick = Button1Click
end
object ADOConnection1: TADOConnection
ConnectionString =
'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initi' +
'al Catalog=mytest;Data Source=host'
LoginPrompt = False
Provider = 'SQLOLEDB.1'
Left = 72
Top = 8
end
object ADOQuery1: TADOQuery
Connection = ADOConnection1
CursorType = ctStatic
Parameters = <>
SQL.Strings = (
'select s.sno 学号,sname 姓名,ssex 性别,sage 年龄,sdept 所在系,sc.cno 课程号,gr' +
'ade 分数,cname 课程名,ccredit 学分,semster 学期 from student s join sc on' +
' s.sno=sc.sno join course c on sc.cno=c.cno where 1=2')
Left = 104
Top = 8
end
object DataSource1: TDataSource
DataSet = ADOQuery1
Left = 152
Top = 8
end
end