关于动态查询???(200分)

  • 主题发起人 主题发起人 金澍
  • 开始时间 开始时间

金澍

Unregistered / Unconfirmed
GUEST, unregistred user!
针对数据库的一个关键字段:
1。在一个EDIT中输入,出现一个窗口进行模糊查找,
2。用光标键能切换到查询窗口进行选择
3。返回字段内容至EDIT
 
请说的明白一点,有点看不明白
 
1、模糊查询用“like"匹配字段就行了。
 
用sql语句中的LIKE ,%,具体查SQL文挡
 
你确定返回一条记录吗?
如果是多条怎么放到edit中?
建议用dbgrid显示记录...

query1.close;
query1.sql.clear;
query1.sql.add('select youe_fieldname from your_database where condition_field='+'"'+edit1.text+'"');//edit中是字符串...
query1.open;

 
select fields ...
from tables
where fiels like "yourstring"
 
dataset.sql.add('select *from tablename where fieldvalue like:value')
dataset.parambyname('value').asstring:=edit1.text;
这样不就可以了吗?
 
用findnerest 不是更好吗?反正是 关键字段
 
同乙低手,我也是这么做的!
 
对呀,
dataset.sql.add('select *from tablename where fieldvalue like:value')
dataset.parambyname('value').asstring:=edit1.text;
这样不就可以了吗?
 
举个例子吧。
with Qurey1 do
begin
close;
SQL.clear;
SQL.Add('select * from tablename where fieldname=:var1');
SQL.parambyname('var1').asstring:=edit1.text;
prepare;
open;
end
 
1.select fields ...
from tables
where fiels like '''%'+edit1.text+'%'''
2.选择查询结果时
edit1.text:=query1['field'];
 
具体实现方法(仅供参考,不知是否符合您的要求):
在窗体上画上Edit1,dbgrid1,datasource1,table1
将dbgrid1,datasource1,table1与数据库作好连接并激活table1
Edit1的事件的 OnChange 代码如下:
table1.FindNearest([Edit1.Text]);
Edit1的事件的 OnChange 代码如下:
if (Key = vk_Return) then
DBGrid1.SetFocus;//输入模糊查找内容,敲回车进入bgrid1,用光标键在查询窗口进行选择
如果想返回字段内容至EDIT1,只要将dbgrid1 的 OnKeyUp 事件加如下代码:
edit1.text:=table1.FieldByName('您的关键字段名称').asstring ;



 
我有一公用查询程序,可能符合要求。
object FrmGetData: TFrmGetData
Left = 189
Top = 109
Width = 376
Height = 309
Caption = '资料查询'
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'System'
Font.Style = [fsBold]
Position = poScreenCenter
OnActivate = FormActivate
OnClose = FormClose
PixelsPerInch = 96
TextHeight = 16
object Panel1: TPanel
Left = 0
Top = 0
Width = 368
Height = 33
Align = alTop
TabOrder = 0
object DBNavigator1: TDBNavigator
Left = 1
Top = 1
Width = 366
Height = 31
VisibleButtons = [nbFirst, nbPrior, nbNext, nbLast]
Align = alClient
TabOrder = 0
end
end
object Panel2: TPanel
Left = 0
Top = 241
Width = 368
Height = 41
Align = alBottom
TabOrder = 1
object CmdOk: TButton
Left = 56
Top = 8
Width = 75
Height = 25
Caption = '&O 确定'
TabOrder = 0
OnClick = CmdOkClick
end
object CmdCancel: TButton
Left = 238
Top = 8
Width = 75
Height = 25
Caption = '&C 取消'
TabOrder = 1
OnClick = CmdCancelClick
end
end
object Panel3: TPanel
Left = 0
Top = 33
Width = 368
Height = 208
Align = alClient
TabOrder = 2
object DBGrid1: TDBGrid
Left = 1
Top = 1
Width = 366
Height = 206
Align = alClient
TabOrder = 0
TitleFont.Charset = GB2312_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -16
TitleFont.Name = 'System'
TitleFont.Style = [fsBold]
OnDblClick = DBGrid1DblClick
end
end
end


unit GetData;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DBCtrls, ExtCtrls, Grids, DBGrids, Db, DBTables;

type
TFrmGetData = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
DBNavigator1: TDBNavigator;
CmdOk: TButton;
CmdCancel: TButton;
DBGrid1: TDBGrid;
procedure FormActivate(Sender: TObject);
procedure CmdOkClick(Sender: TObject);
procedure CmdCancelClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure DBGrid1DblClick(Sender: TObject);
private
DS:TDatasource;
public
property mydatasource:TDatasource read Ds write ds;
function show:boolean;
function getvalue(fname: string):variant;
procedure init;
end;

var
FrmGetData: TFrmGetData;
okornot:boolean;

implementation

{$R *.DFM}

procedure TFrmGetData.FormActivate(Sender: TObject);
begin
dbgrid1.DataSource:=mydatasource;
dbnavigator1.DataSource:=mydatasource;
end;

procedure TFrmGetData.CmdOkClick(Sender: TObject);
begin
okornot:=true;
close;
end;

procedure TFrmGetData.CmdCancelClick(Sender: TObject);
begin
okornot:=false;
close;
end;
function Tfrmgetdata.show:boolean;
begin
showmodal;
result:=okornot;
end;
function Tfrmgetdata.getvalue(fname: string):variant;
var i:integer;
begin
with mydatasource.DataSet do
begin
for i := 0 to fieldcount - 1 do
if uppercase(fields.fieldname)=
uppercase(fname)
then
result:=fields.value;
end;
end;
procedure Tfrmgetdata.init;
begin
application.CreateForm(Tfrmgetdata, frmgetdata);
end;
procedure TFrmGetData.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;

procedure TFrmGetData.DBGrid1DblClick(Sender: TObject);
begin
okornot:=true;
close;
end;

end.

调用:
procedure TFDeSalary.E_dptNoDblClick(Sender: TObject);
begin
Query1.Open;// Query1<--DateSoure1

frmgetdata.init;
frmgetdata.mydatasource:=DateSoure1;
if Frmgetdata.show then
E_dptNo.Text:=Frmgetdata.getvalue('dptNo');
end;

// Query1: select * from tablename where fieldname like %+edit1.text
//fieldname 需是 char 型。
 
使用如下函数吧!(Delphi 1, Delphi 2 不支持)

function TDateSet.Locate(const KeyFields: String;
const KeyValues: Variant;
Options: TLocateOptions): Boolean;

 
多人接受答案了。
 
后退
顶部