手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

Delphi 与SQL编程(二)

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

if strField <> '' then

with Table1.FieldByName(strField) do

if (DataType = ftString) or (DataType = ftMemo) then

strQuote := '"' else

strQuote := '';

 

{ Construct the WHERE clause of the query based on the user's choice

in Type. }

 

case etSQLOps(ComboBox1.ItemIndex) of

soNoCondition: strWhere := '';

soEqual: strWhere := strField ' = ' strQuote strValue strQuote;

soNotEqual: strWhere := strField ' <> ' strQuote strValue

strQuote;

soLessThan: strWhere := strField ' < ' strQuote strValue

strQuote;

soLessEqual: strWhere := strField ' <= ' strQuote strValue

strQuote;

soMoreThan: strWhere := strField ' > ' strQuote strValue

strQuote;

soMoreEqual: strWhere := strField ' >= ' strQuote strValue

strQuote;

soStartsWith: strWhere := strField ' LIKE ' strQuote

strValue '%' strQuote;

soNoStartsWith: strWhere := strField ' NOT LIKE ' strQuote

strValue '%' strQuote;

soEndsWith: strWhere := strField ' LIKE ' strQuote

'%' strValue strQuote;

soNoEndsWith: strWhere := strField ' NOT LIKE '

strQuote '%' strValue strQuote;

soContains: strWhere := strField ' LIKE ' strQuote '%' strValue

'%' strQuote;

soNoContains: strWhere := strField ' NOT LIKE ' strQuote '%'

strValue '%' strQuote;

soBlank: strWhere := strField ' IS NULL';

soNotBlank: strWhere := strField ' IS NOT NULL';

end;

 

if ComboBox1.ItemIndex = Ord(soNoCondition) then

strQuery := 'SELECT * FROM "' strTable '"'

else if Table1.FieldByName(strField).DataType = ftString then

strQuery := 'SELECT * FROM "' strTable '" t WHERE t.' strWhere

else

strQuery := 'SELECT * FROM "' strTable '" t WHERE t.' strWhere;

 

{ Create an instance of the browser form. }

frmQuery := TResultForm.Create(Application);

 

{ Use a resource protection block in case an exception is raised. This

ensures that the memory allocated for the Results form is released. }

try

with frmQuery do

begin

Screen.Cursor := crHourglass;

if Query1.Active then Query1.Close;

Query1.DatabaseName := strAlias; {set the alias the query poitns to}

Query1.SQL.clear; { empty existing SQL in the query }

Query1.SQL.Add(strQuery); { add query string to query object }

Query1.Active := True; { try to run the query }

Screen.Cursor := crDefault;

 

if Query1.Active then

begin

{ If the query didn't return any records, there's no point in

displaying the form. In that event, raise an exception. }

if Query1.RecordCount < 1 then

raise Exception.create('No records matched your criteria.

Please try again.' );

 

{ write a message to the browse form's status line }

if strField = '' then

Panel3.Caption := 'Now showing all records from ' strTable

'...'

else

Panel3.Caption := 'Now showing ' strTable ' where ' strField

' contains values equal to ' strValue '...';

 

{ show the form }

ShowModal;

end;

end;

finally

frmQuery.Free;

end;

end;

 

end.

 

 

unit RSLTFORM;

 

interface

 

uses

SysUtils, Windows, Messages, Classes, Graphics, Controls, StdCtrls, DB,

Forms, DBCtrls, DBGrids, DBTables, Buttons, Grids, ExtCtrls, Dialogs;

 

type

TResultForm = class(TForm)

DBGrid1: TDBGrid;

DBNavigator: TDBNavigator;

Panel1: TPanel;

DataSource1: TDataSource;

Panel2: TPanel;

Panel3: TPanel;

Query1: TQuery;

SpeedButton2: TSpeedButton;

Panel4: TPanel;

SpeedButton1: TSpeedButton;

procedure SpeedButton1Click(Sender: TObject);

procedure SpeedButton2Click(Sender: TObject);

end;

 

var

ResultForm: TResultForm;

 

implementation

 

{$R *.DFM}

 

procedure TResultForm.SpeedButton1Click(Sender: TObject);

begin

Close;

end;

 

procedure TResultForm.SpeedButton2Click(Sender: TObject);

var

strText: string; { Variable to hold display text }

iCounter: Integer; { Loop counter variable }

begin

 

{ Build a string containing the query }

 

strText := '';

for iCounter := 0 to Query1.SQL.Count - 1 do

strText := strText Query1.SQL[iCounter];

 

{ Display the query text }

 

MessageDlg('The underlying query is: ' #10 #10 strText,

mtInformation, [mbOK], 0 );

end;

 

end.

上一篇: Delphi客户服务器应用开发(一)

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!