电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> delphi技术>>Delphi 与SQL编程(二)(3):

Delphi 与SQL编程(二)(3)

来源:远方网络 | 2005-12-31 9:40:35 | (有2077人读过)

Table1.Open;

if Table1.Active then

Table1.GetFieldNames(ListBox3.Items);

finally

Screen.Cursor := crDefault;

end;

end; 

procedure TQueryForm.BitBtn2Click(Sender: TObject);

var

strAlias, { Alias name selected by the user }

strTable, { Table name selected by the user }

strField, { Field name selected by the user }

strValue, { Field Value entered by the user }

strWhere, { WHERE clause for the user's query }

strQuote, { Holds quotes is the query field is text }

strQuery: string; { String used to construct the query }

frmQuery: TResultForm; { The Results form }

type 

{ The following type is used with the Type drop-downlist. The text values corresponding with each item is described in comments, along with the relevant SQL operators. }

etSQLOps = (soNoCondition, { not field conditions: no WHERE clause }

soEqual, { equals: = }

soNotEqual, { is not equal to: <> }

soLessThan, { is less than: < }

soLessEqual, { is less than or equal to: <= }

soMoreThan, { is greater than: > }

soMoreEqual, { is greater than or equal to: >= }

soStartsWith, { starts with: LIKE xx% }

soNoStartsWith, { doesn't start with: NOT LIKE xx% }

soEndsWith, { ends with: LIKE %xx }

soNoEndsWith, { doesn't end with: NOT LIKE %xx }

soContains, { contains: LIKE %xx% }

soNoContains, { doesn't contain: NOT LIKE %xx% }

soBlank, { is blank: }

soNotBlank, { is not blank: }

soInside, { contains only: IN ( xx, yy, zz ) }

soOutside); { doesn't contain: NOT IN (xx, yy, zz) }

begin 

{ Initialize the variables needed to run the query }

with ListBox1 do

if ItemIndex = -1 then

raise Exception.Create('Can''t Run Query: No Alias Selected')

else

strAlias := Items.Strings[ItemIndex];

 

with ListBox2 do

if ItemIndex = -1 then

raise Exception.Create('Can''t Run Query: No Table Selected')

else

strTable := Items.Strings[ItemIndex]; 

with ListBox3 do

if ItemIndex = -1 then

begin

if ComboBox1.ItemIndex > Ord(soNocondition) then

raise Exception.Create('Can''t Run Query: No Field Selected')

else

strField := '';

end

else

strField := Items.Strings[ItemIndex]; 

if (Edit1.Text = '') and

(ComboBox1.ItemIndex > Ord(soNoCondition)) and

(ComboBox1.ItemIndex < Ord(soBlank)) then

raise Exception.create('Can''t Run Query: No Search Value Entered')

else

strValue := Edit1.Text; 

{ See if the field being search is a string field. If so, then pad the

quote string with quotation marks; otherwise, set it to a null value. } 

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



delphi技术热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号