熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

DELPHI基礎教程:SQL編程(二)[3]

2013-11-23 17:49:07  來源: Delphi編程 

  TableOpen;

  if TableActive then

  TableGetFieldNames(ListBoxItems)

  finally

  ScreenCursor := crDefault;

  end;

  end;

  procedure TQueryFormBitBtnClick(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 users 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 dropdown

  list 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 { doesnt start with: NOT LIKE xx% }

  soEndsWith { ends with: LIKE %xx }

  soNoEndsWith { doesnt end with: NOT LIKE %xx }

  soContains { contains: LIKE %xx% }

  soNoContains { doesnt contain: NOT LIKE %xx% }

  soBlank { is blank: }

  soNotBlank { is not blank: }

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

  soOutside) { doesnt contain: NOT IN (xx yy zz) }

  begin

  { Initialize the variables needed to run the query }

  with ListBox do

  if ItemIndex = then

  raise ExceptionCreate(Cant Run Query: No Alias Selected

  else

  strAlias := ItemsStrings[ItemIndex];

  with ListBox do

  if ItemIndex = then

  raise ExceptionCreate(Cant Run Query: No Table Selected

  else

  strTable := ItemsStrings[ItemIndex];

  with ListBox do

  if ItemIndex = then

  begin

  if ComboBoxItemIndex > Ord(soNocondition) then

  raise ExceptionCreate(Cant Run Query: No Field Selected

  else

  strField := ;

  end

  else

  strField := ItemsStrings[ItemIndex];

  if (EditText = ) and

  (ComboBoxItemIndex > Ord(soNoCondition)) and

  (ComboBoxItemIndex < Ord(soBlank)) then

  raise Exceptioncreate(Cant Run Query: No Search Value Entered

  else

  strValue := EditText;

  { 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 TableFieldByName(strField) do

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

  strQuote := else

  strQuote := ;

  { Construct the WHERE clause of the query based on the users choice

  in Type }

  case etSQLOps(ComboBoxItemIndex) 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 ComboBoxItemIndex = Ord(soNoCondition) then

  strQuery := SELECT * FROM + strTable +

  else if TableFieldByName(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 := TResultFormCreate(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

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/Delphi/201311/25142.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.