with query do begin close; sql.clear; sql.add('select * from Table') sql.add('where condition1=:param1 and condition2=:param2 ....'); ParamByName('param1').value := edit1.text; paramByname('param2').value :=edit2.text; open; end; 注意类型转换
将输入条内的字符串赋值给Adoquery的sql,然后将adoquery激活就可以进行查询了呀
Form上的控件: DataSource1:TDataSource; DBGrid1:TDBGrid1; ADOQuery1:TQuery; Edit1:TEdit; Button1:TButton;DataSource1.DataSet=ADOQuery1; DBGrid1.DataSource=DataSource1在Button1的OnClick事件中 ADOQuery1.ConnectionString:='....';//你可以在设计时生成 ADOQuery1.SQL.Add('Select * From mytable Where myField=:mydata'); ADOQuery1.Params.ParambyName('mydata').AsString:=Edit1.Text; ADOQuery1.Active:=True;
比如说有3个Edit框,要查的表是AA,则用控件Query 语句 : with query do begin Close; //关闭这个Query SQL.Clear; //清空当前的SQL语句 SQL.Add('select * from AA (nolock)'); //下面是嵌套的SQL语句。 SQL.Add('where 1=1'); //默认所有的 if edit1.text <> '' then //下面是根据条件筛选 SQL.Add('and XX = '''+edit1.text+''''); if edit2.text <> '' then SQL.Add('and XX = '''+edit2.text+''''); if edit3.text <> '' then SQL.Add('and XX = '''+edit3.text+''''); Open; // 运行这个SQL语句。 end; 以上是查询时的语句,如果是删除,修改或插入,则要把Open改为Execsql 其它的语法和上面一样! 只是改成Insert,Update,Delete
我貼! library FindDll;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses ShareMem, SysUtils, Classes, UtFind in 'UtFind.pas' {FindFrm};{$R *.res} exports FindShow; begin end. unit UtFind;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, DB, DBTables, ADODB, Buttons;type TFindFrm = class(TForm) ComboBox1: TComboBox; ComboBox2: TComboBox; Edit1: TEdit; GroupBox1: TGroupBox; RadioButton1: TRadioButton; RadioButton2: TRadioButton; RichEdit1: TRichEdit; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Query1: TADOQuery; ADOConnection1: TADOConnection; ListBox1: TListBox; SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; ListBox2: TListBox; Label1: TLabel; RichEdit2: TRichEdit; RadioButton3: TRadioButton; RadioButton4: TRadioButton; procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure FormShow(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ListBox1DblClick(Sender: TObject); procedure ListBox2DblClick(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton2Click(Sender: TObject); private { Private declarations } TBname:string; //數據庫名稱 FindSqlString:string; //返回的查詢字符串 public { Public declarations } end;//定義出口函數 function FindShow(H:THandle;TName:string):string;stdcall;implementation{$R *.dfm}function FindShow(H:THandle;TName:string):string; var FindFrm: TFindFrm; begin application.Handle:=H; FindFrm:=TFindFrm.Create(application); try FindFrm.Query1.SQL.Clear; FindFrm.Query1.SQL.Add(TName); FindFrm.ShowModal; Result:=FindFrm.FindSqlString; finally FindFrm.Free; end; end;procedure TFindFrm.Button1Click(Sender: TObject); var aa,bb :string; var c:integer; begin if RichEdit1.Lines.Text='' then aa:='' else begin if RadioButton1.Checked=True then aa:='AND' else aa:='OR'; end; c:=Pos(' ',ComboBox1.Text); RichEdit2.Text:=RichEdit2.Text+' '+aa+' '+'('+copy(ComboBox1.Text,1,c-1)+' '+ComboBox2.Text+' '+''''+Edit1.Text+''')'; RichEdit1.Text:=RichEdit1.Text+' '+aa+' '+'('+copy(ComboBox1.Text,c+1,Length(ComboBox1.Text)-c-1)+' '+ComboBox2.Text+' '+''''+Edit1.Text+''')'; end; procedure TFindFrm.Button3Click(Sender: TObject); var i,n:integer; var OrderStr,ListStr:string; begin //把生成的sql語句添加到FindSqlString中 if RichEdit1.Lines.Text<>'' then FindSqlString:='SELECT * FROM '+trim(TBname)+' WHERE '+trim(RichEdit2.Text) else FindSqlString:='SELECT * FROM '+trim(TBname); //加orderby if ListBox2.Items.Text<>'' then for i:=0 to ListBox2.Items.Count-1 do begin n:=Pos(' ',ListBox2.Items.Strings[i]); ListStr:=copy(ListBox2.Items.Strings[i],1,n-1); if i=0 then OrderStr:=ListStr else OrderStr:=OrderStr+','+ListStr; end; //加dsc if OrderStr<>'' then begin if RadioButton3.Checked=True then OrderStr:=' ORDER BY '+OrderStr+' ASC' else OrderStr:=' ORDER BY '+OrderStr+' DESC'; end; FindSqlString:=FindSqlString+OrderStr; self.Close; end;procedure TFindFrm.Button4Click(Sender: TObject); begin FindSqlString:=''; self.Close; end;procedure TFindFrm.FormShow(Sender: TObject); var i:integer; var a:Tobject; begin TBname:=Query1.SQL.Text; Query1.SQL.Clear; Query1.SQL.Add('SELECT MA003,MA004 FROM XADMA WHERE MA001='''+Trim(TBname)+''''); RichEdit1.Lines.Text:=''; if Query1.Active=False then Query1.Open; while not Query1.Eof do begin ComboBox1.AddItem(trim(Query1.FieldValues['MA003'])+' '+Query1.FieldValues['MA004'],a); ListBox1.AddItem(trim(Query1.FieldValues['MA003'])+' '+Query1.FieldValues['MA004'],a); Query1.Next; end; Query1.Close; ComboBox1.ItemIndex:=0; end;procedure TFindFrm.Button2Click(Sender: TObject); begin RichEdit2.Text:=''; RichEdit1.Text:=''; end;procedure TFindFrm.ListBox1DblClick(Sender: TObject); var a:Tobject; begin if ListBox1.ItemIndex<>-1 then begin ListBox2.AddItem(ListBox1.Items.Strings[ListBox1.ItemIndex],a); ListBox1.DeleteSelected; end; end;procedure TFindFrm.ListBox2DblClick(Sender: TObject); var a:Tobject; begin if ListBox2.ItemIndex<>-1 then begin ListBox1.AddItem(ListBox1.Items.Strings[ListBox2.ItemIndex],a); ListBox2.DeleteSelected; end; end;procedure TFindFrm.SpeedButton1Click(Sender: TObject); begin ListBox1DblClick(Sender); end;procedure TFindFrm.SpeedButton2Click(Sender: TObject); begin ListBox2DblClick(Sender); end;end.俺這個dll到處都可以用,如果要的話,俺可以給個demo給你
begin
close;
sql.clear;
sql.add('select * from Table')
sql.add('where condition1=:param1 and condition2=:param2 ....');
ParamByName('param1').value := edit1.text;
paramByname('param2').value :=edit2.text;
open;
end;
注意类型转换
DataSource1:TDataSource;
DBGrid1:TDBGrid1;
ADOQuery1:TQuery;
Edit1:TEdit;
Button1:TButton;DataSource1.DataSet=ADOQuery1;
DBGrid1.DataSource=DataSource1在Button1的OnClick事件中
ADOQuery1.ConnectionString:='....';//你可以在设计时生成
ADOQuery1.SQL.Add('Select * From mytable Where myField=:mydata');
ADOQuery1.Params.ParambyName('mydata').AsString:=Edit1.Text;
ADOQuery1.Active:=True;
语句 :
with query do
begin
Close; //关闭这个Query
SQL.Clear; //清空当前的SQL语句
SQL.Add('select * from AA (nolock)'); //下面是嵌套的SQL语句。
SQL.Add('where 1=1'); //默认所有的
if edit1.text <> '' then //下面是根据条件筛选
SQL.Add('and XX = '''+edit1.text+'''');
if edit2.text <> '' then
SQL.Add('and XX = '''+edit2.text+'''');
if edit3.text <> '' then
SQL.Add('and XX = '''+edit3.text+'''');
Open; // 运行这个SQL语句。
end;
以上是查询时的语句,如果是删除,修改或插入,则要把Open改为Execsql
其它的语法和上面一样!
只是改成Insert,Update,Delete
library FindDll;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
ShareMem,
SysUtils,
Classes,
UtFind in 'UtFind.pas' {FindFrm};{$R *.res}
exports
FindShow;
begin
end.
unit UtFind;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, DBTables, ADODB, Buttons;type
TFindFrm = class(TForm)
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Edit1: TEdit;
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RichEdit1: TRichEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Query1: TADOQuery;
ADOConnection1: TADOConnection;
ListBox1: TListBox;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
ListBox2: TListBox;
Label1: TLabel;
RichEdit2: TRichEdit;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox2DblClick(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private
{ Private declarations }
TBname:string; //數據庫名稱
FindSqlString:string; //返回的查詢字符串
public
{ Public declarations }
end;//定義出口函數
function FindShow(H:THandle;TName:string):string;stdcall;implementation{$R *.dfm}function FindShow(H:THandle;TName:string):string;
var
FindFrm: TFindFrm;
begin
application.Handle:=H;
FindFrm:=TFindFrm.Create(application);
try
FindFrm.Query1.SQL.Clear;
FindFrm.Query1.SQL.Add(TName);
FindFrm.ShowModal;
Result:=FindFrm.FindSqlString;
finally
FindFrm.Free;
end;
end;procedure TFindFrm.Button1Click(Sender: TObject);
var aa,bb :string;
var c:integer;
begin
if RichEdit1.Lines.Text='' then
aa:=''
else
begin
if RadioButton1.Checked=True then
aa:='AND'
else
aa:='OR';
end;
c:=Pos(' ',ComboBox1.Text);
RichEdit2.Text:=RichEdit2.Text+' '+aa+' '+'('+copy(ComboBox1.Text,1,c-1)+' '+ComboBox2.Text+' '+''''+Edit1.Text+''')';
RichEdit1.Text:=RichEdit1.Text+' '+aa+' '+'('+copy(ComboBox1.Text,c+1,Length(ComboBox1.Text)-c-1)+' '+ComboBox2.Text+' '+''''+Edit1.Text+''')';
end;
procedure TFindFrm.Button3Click(Sender: TObject);
var i,n:integer;
var OrderStr,ListStr:string;
begin
//把生成的sql語句添加到FindSqlString中
if RichEdit1.Lines.Text<>'' then
FindSqlString:='SELECT * FROM '+trim(TBname)+' WHERE '+trim(RichEdit2.Text)
else
FindSqlString:='SELECT * FROM '+trim(TBname);
//加orderby
if ListBox2.Items.Text<>'' then
for i:=0 to ListBox2.Items.Count-1 do
begin
n:=Pos(' ',ListBox2.Items.Strings[i]);
ListStr:=copy(ListBox2.Items.Strings[i],1,n-1);
if i=0 then
OrderStr:=ListStr
else
OrderStr:=OrderStr+','+ListStr;
end;
//加dsc
if OrderStr<>'' then
begin
if RadioButton3.Checked=True then
OrderStr:=' ORDER BY '+OrderStr+' ASC'
else
OrderStr:=' ORDER BY '+OrderStr+' DESC';
end;
FindSqlString:=FindSqlString+OrderStr;
self.Close;
end;procedure TFindFrm.Button4Click(Sender: TObject);
begin
FindSqlString:='';
self.Close;
end;procedure TFindFrm.FormShow(Sender: TObject);
var i:integer;
var a:Tobject;
begin
TBname:=Query1.SQL.Text;
Query1.SQL.Clear;
Query1.SQL.Add('SELECT MA003,MA004 FROM XADMA WHERE MA001='''+Trim(TBname)+'''');
RichEdit1.Lines.Text:='';
if Query1.Active=False then Query1.Open;
while not Query1.Eof do
begin
ComboBox1.AddItem(trim(Query1.FieldValues['MA003'])+' '+Query1.FieldValues['MA004'],a);
ListBox1.AddItem(trim(Query1.FieldValues['MA003'])+' '+Query1.FieldValues['MA004'],a);
Query1.Next;
end;
Query1.Close;
ComboBox1.ItemIndex:=0;
end;procedure TFindFrm.Button2Click(Sender: TObject);
begin
RichEdit2.Text:='';
RichEdit1.Text:='';
end;procedure TFindFrm.ListBox1DblClick(Sender: TObject);
var a:Tobject;
begin
if ListBox1.ItemIndex<>-1 then
begin
ListBox2.AddItem(ListBox1.Items.Strings[ListBox1.ItemIndex],a);
ListBox1.DeleteSelected;
end;
end;procedure TFindFrm.ListBox2DblClick(Sender: TObject);
var a:Tobject;
begin
if ListBox2.ItemIndex<>-1 then
begin
ListBox1.AddItem(ListBox1.Items.Strings[ListBox2.ItemIndex],a);
ListBox2.DeleteSelected;
end;
end;procedure TFindFrm.SpeedButton1Click(Sender: TObject);
begin
ListBox1DblClick(Sender);
end;procedure TFindFrm.SpeedButton2Click(Sender: TObject);
begin
ListBox2DblClick(Sender);
end;end.俺這個dll到處都可以用,如果要的話,俺可以給個demo給你