我做查詢是用dll做的 提供數據庫名稱 讓dll中的form生成查詢語句 然後返回給應用程序 —————————————————————————— 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.調用時的代碼 Fstr:=FindShow(application.Handle,TableName);
提供數據庫名稱
讓dll中的form生成查詢語句
然後返回給應用程序
——————————————————————————
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.調用時的代碼
Fstr:=FindShow(application.Handle,TableName);
我也一直在找Delphi的通用查询
找到了, 好东西大家分享!!!