程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 執行SQL語句查詢的Delphi例子

執行SQL語句查詢的Delphi例子

編輯:Delphi

Delphi SQL查詢例子,模擬的SQL查詢分析器的功能,若數據庫連接正確,SQL順利執行的話,會看到數據顯示,統計出記錄集條數,Delphi高手請跳過。

vIEw source print? 01 unit Unit1; 02 interface 03 uses 04   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 05   Dialogs, StdCtrls, ComCtrls, DB, ADODB, ExtCtrls, Grids, DBGrids; 06 type 07   TForm1 = class(TForm) 08     GroupBox1: TGroupBox; 09     EditSQL: TEdit; 10     ButtonSQL: TButton; 11     ButtonClear: TButton; 12     DBGrid1: TDBGrid; 13     Timer1: TTimer; 14     ADOConnection1: TADOConnection; 15     ADOQuery1: TADOQuery; 16     DataSource1: TDataSource; 17     StatusBar1: TStatusBar; 18     Label1: TLabel; 19     Label2: TLabel; 20     EditTotalNum: TEdit; 21     procedure Timer1Timer(Sender: TObject); 22     procedure ButtonClearClick(Sender: TObject); 23     procedure ButtonSQLClick(Sender: TObject); 24   private 25     { Private declarations } 26   public 27     { Public declarations } 28   end; 29 var 30   Form1: TForm1; 31 implementation 32 {$R *.dfm} 33 procedure TForm1.Timer1Timer(Sender: TObject); 34 begin 35   ShortDateFormat:='y/m/d h:n:ss'; 36   StatusBar1.Panels.Items[0].Text:='時間:'+DateToStr(now); 37 end; 38 procedure TForm1.ButtonClearClick(Sender: TObject); 39 begin 40   EditSQL.Text:=''; 41 end; 42 procedure TForm1.ButtonSQLClick(Sender: TObject); 43 var 44   AffectRecordCount:Integer; 45   SQLString:string; 46 begin 47   SQLString:=Trim(EditSQL.Text); 48   try 49     with ADOQuery1 do 50       begin 51         close; 52         sql.Clear; 53         sql.add(SQLString); 54         if UpperCase( copy(SQLString,1,6 )) = 'SELECT' then 55           begin 56             open; 57             StatusBar1.Panels.Items[1].Text:='狀態:'+'SELECT語句執行成功,返回記錄總數:' 58                                          +IntToStr(RecordCount); 59             EditTotalNum.Text:=IntToStr(RecordCount); 60           end 61         else 62           begin 63             AffectRecordCount:=ExecSQL; 64             StatusBar1.Panels.Items[1].Text:='狀態:'+'SQL語句執行成功,受影響記錄個數:' 65                                            +IntToStr(AffectRecordCount); 66             if MessageDlg('需要提取更新的數據嗎?',mtInformation,[mbYes,mbNo],0)=mrYes then 67               begin 68                 close; 69                 sql.Clear; 70                 sql.add('select * from Contact'); 71                 open; 72                 EditTotalNum.Text:=IntToStr(RecordCount); 73               end; 74           end; 75       end; 76   except 77     on E:Exception do 78       begin 79         StatusBar1.Panels.Items[1].Text:='狀態:'+'SQL語句執行失敗--'+e.Message; 80         Exit; 81       end; 82   end; 83 end; 84 end.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved