⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 delphi自动出题系统源代码,有兴趣的可以来看看!
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ComCtrls, Tabnotbk, Db, DBTables;

type
  Tgenerateform = class(TForm)
    TabbedNotebook1: TTabbedNotebook;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ListBox1: TListBox;
    ListBox2: TListBox;
    ListBox3: TListBox;
    Label4: TLabel;
    Edit1: TEdit;
    Label5: TLabel;
    ComboBox1: TComboBox;
    Label6: TLabel;
    Edit2: TEdit;
    Memo1: TMemo;
    Label7: TLabel;
    Table1: TTable;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure ListBox2Click(Sender: TObject);
    procedure ListBox3Click(Sender: TObject);
    procedure generateSQL;
    procedure ComboBox1Exit(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  generateform: Tgenerateform;
  strAlias,
  strTable,
  strField,
  strQuery:string;
type
   etSQLOps=(soNoCondition,
              soEqual,
              soNotEqual,
              soLessThan,
              soLessEqual,
              soMoreThan,
              soMoreEqual,
              soContains,
              soNocontains,
              soBlank,
              soNotBlank);

implementation
uses unit2, main;

{$R *.DFM}

procedure Tgenerateform.BitBtn2Click(Sender: TObject);
begin
  close;
  mainform.show;
end;

procedure Tgenerateform.FormCreate(Sender: TObject);
begin
    screen.Cursor:=crHourglass;
    with listbox1 do
    begin
    items.Clear;
    session.GetAliasNames(items);
    end;
    screen.Cursor:=crDefault;
    if listbox1.Items.count<1 then
    messagedlg('这里没有定义好的数据库别名。你'+'至少需要一个可以使用的别名。',mtError,[mbOK],0);
    
end;

procedure Tgenerateform.ListBox1Click(Sender: TObject);
 var
    strValue:string;
    bIslocal:boolean;
    slParams:tstringList;
    iCounter:integer;
begin
      with listbox1 do
      strvalue:=items.Strings[itemindex];
      session.GetTableNames(strvalue,'',checkbox1.checked,checkbox2.checked,listbox2.items);
      screen.Cursor:=crdefault;
      if listbox2.Items.count<1 then
      messagedlg('在你选择的数据库中没有表,请选择其他的数据库别名',mtError,[mbOK],0);
      listbox3.Items.Clear;
      
end;

procedure Tgenerateform.ListBox2Click(Sender: TObject);
begin
    screen.Cursor:=crhourglass;
    if table1.active then
    table1.close;
    with listbox1 do
    table1.databasename:=items.Strings[itemindex];
    with listbox2 do
      table1.TableName:=items.Strings[itemindex];
      table1.Open;
      if table1.Active then
      table1.GetFieldNames(listbox3.items);
      screen.Cursor:=crdefault;

end;

procedure Tgenerateform.ListBox3Click(Sender: TObject);
begin
   edit1.text:=listbox3.Items.Strings[listbox3.itemindex];
end;
procedure Tgenerateform.generateSQL;
var
   strValue,
   strWhere,
   strQuote:string;
begin
     with listbox1 do
     if itemindex=-1 then
     exit
     else
      strAlias:=items.Strings[itemindex];
      with listbox2 do
      if itemindex=-1 then
      exit
      else
        strtable:=items.Strings[itemindex];
        with listbox3 do
        if itemindex=-1 then
        begin
          if combobox1.ItemIndex>ord(soNocondition) then
          exit
          else
          strField:='';
          end
          else
             strfield:=items.Strings[itemindex];
             if (edit2.text='') and
             (combobox1.ItemIndex>ord(sonocondition)) and
             (combobox1.ItemIndex<ord(soblank)) then
             exit
             else
               strValue:=edit2.Text;
          if strfield<>'' then
          with table1.FieldByName(strfield) do
          if (datatype=ftstring) or (datatype=ftMemo) then
          strQuote:='"' else
          strQuote:='';
          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;
            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+'" where '+strwhere
              else
              strquery:='SELECT * FROM "'+strtable+'" where '+strwhere;
              memo1.Lines.text:=strQuery;
end;

procedure Tgenerateform.ComboBox1Exit(Sender: TObject);
begin
   generateSQL;
end;

procedure Tgenerateform.BitBtn1Click(Sender: TObject);
begin
   with listbox1 do
   if itemindex=-1 then
   raise exception.Create('不能执行查询,没有选择数据库别名!');
   with listbox2 do
   if itemindex=-1  then
   raise exception.Create('不能执行查询,没有选择查询数据表');
   with listbox3 do
   if itemindex=-1 then
   if combobox1.ItemIndex>ord(sonocondition) then
   raise exception.Create('没有选择查询的字段');
   if (edit2.text='')and (combobox1.ItemIndex>ord(sonocondition))
        and(combobox1.ItemIndex<ord(soblank)) then
        raise exception.Create('不能执行查询,没有设置查询的数值!');
   try
     with resultform do
     begin
     screen.Cursor:=crHourglass;
     if query1.Active then
     query1.Close;
     query1.DatabaseName:=strAlias;
     query1.SQL.clear;
     query1.SQL.add(strquery);
     query1.Active:=true;
     screen.Cursor:=crdefault;
     if query1.Active then
     begin
     if query1.RecordCount<1 then
     raise exception.Create('没有符合查询条件的记录,请重新设置查询条件');
     showmodal;
     end;
   end;
   except
     showmessage('查询条件有误!');
     end;
end;

procedure Tgenerateform.SpeedButton1Click(Sender: TObject);
begin
  close;

end;

procedure Tgenerateform.SpeedButton2Click(Sender: TObject);
begin
   with listbox1 do
   if itemindex=-1 then
   raise exception.Create('不能执行查询,没有选择数据库别名!');
   with listbox2 do
   if itemindex=-1  then
   raise exception.Create('不能执行查询,没有选择查询数据表');
   with listbox3 do
   if itemindex=-1 then
   if combobox1.ItemIndex>ord(sonocondition) then
   raise exception.Create('没有选择查询的字段');
   if (edit2.text='')and (combobox1.ItemIndex>ord(sonocondition))
        and(combobox1.ItemIndex<ord(soblank)) then
        raise exception.Create('不能执行查询,没有设置查询的数值!');
   try
     with resultform do
     begin
     screen.Cursor:=crHourglass;
     if query1.Active then
     query1.Close;
     query1.DatabaseName:=strAlias;
     query1.SQL.clear;
     query1.SQL.add(strquery);
     query1.Active:=true;
     screen.Cursor:=crdefault;
     if query1.Active then
     begin
     if query1.RecordCount<1 then
     raise exception.Create('没有符合查询条件的记录,请重新设置查询条件');
     showmodal;
     end;
   end;
   except
     showmessage('查询条件有误!');
     end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -