線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1177
推到 Plurk!
推到 Facebook!

如何設計一支NT Service的程式對資料庫進行存取的動作?

答題得分者是:william
samuelliang
一般會員


發表:2
回覆:5
積分:1
註冊:2005-02-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-02-25 10:51:37 IP:211.20.xxx.xxx 未訂閱
各位先進麻煩解惑: 小弟目前有個案子是需要設計一支NT Service的程式, 第一步是先針對資料庫做讀取的動作 第二不是然後以SOAP function對外部某個系統進行資料同步的工作 最後,同不成功的話則將該資料庫中的資料刪除 小弟目前已經寫好此NT Service的框架,也可以呼叫SOAP function 但無法讀取跟寫入資料庫,請各位先進指點一下好嗎? ===以下是我的源碼的一部份=== unit SyncLDAP; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, SoapRPC, DB, ADODB, Provider; type TSynchronizeLDAP = class(TService) procedure ServiceStart(Sender: TService; var Started: Boolean); procedure ServicePause(Sender: TService; var Paused: Boolean); procedure ServiceContinue(Sender: TService; var Continued: Boolean); procedure ServiceStop(Sender: TService; var Stopped: Boolean); private { Private declarations } public function GetServiceController: TServiceController; override; { Public declarations } end; type TSyncLDAPThread = class(TThread) adoEUserConn: TADOConnection; adoScanIFQry: TADOQuery; DataSetProvider1: TDataSetProvider; adoScanIFQryF_BADGE_NO: TStringField; adoScanIFQryF_ALTER_TYPE: TStringField; procedure ScanSyncIf; procedure ReqsSoapOp; private public procedure Execute; override; end; var SynchronizeLDAP: TSynchronizeLDAP; SyncLDAPThread: TSyncLDAPThread; const connStr: string = 'Provider=MSDAORA.1;Password=eusereuser;User ID=euser;Data Source=engdb'; const scanSQL: string = 'SELECT F_BADGE_NO, F_ALTER_TYPE FROM TMP_USER_TESTLINK'; implementation {$R *.DFM} procedure ServiceController(CtrlCode: DWord); stdcall; begin SynchronizeLDAP.Controller(CtrlCode); end; function TSynchronizeLDAP.GetServiceController: TServiceController; begin Result := ServiceController; end; procedure TSynchronizeLDAP.ServiceStart(Sender: TService; var Started: Boolean); begin SyncLDAPThread := TSyncLDAPThread.Create(False); Started := True; ReportStatus; end; procedure TSynchronizeLDAP.ServicePause(Sender: TService; var Paused: Boolean); begin SyncLDAPThread.Suspend; Paused := True; ReportStatus; end; procedure TSynchronizeLDAP.ServiceContinue(Sender: TService; var Continued: Boolean); begin SyncLDAPThread.Resume; Continued := True; ReportStatus; end; procedure TSynchronizeLDAP.ServiceStop(Sender: TService; var Stopped: Boolean); begin SyncLDAPThread.Terminate; Stopped := True; ReportStatus; end; procedure TSyncLDAPThread.Execute; begin while not Terminated do begin // Synchronize(Self.ScanSyncIf); // Synchronize(Self.ReqsSoapOp); Beep; Sleep(2000); end; end; procedure TSyncLDAPThread.ScanSyncIf; begin self.adoEUserConn.ConnectionString := connStr; self.adoEUserConn.LoginPrompt := false; self.adoEUserConn.KeepConnection := true; self.adoScanIFQry.Connection := self.adoEUserConn; self.adoScanIFQry.SQL.Text := scanSQL; self.adoScanIFQry.Prepared := true; self.adoScanIFQry.Active := true; ShowMessage('xxxx'); self.adoScanIFQry.Active := false; self.adoEUserConn.KeepConnection := false; end; procedure TSyncLDAPThread.ReqsSoapOp; var SoapResult: OleVariant; begin SoapResult := GetIEService.ping; ShowMessage(SoapResult); end; end.
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-02-25 11:13:00 IP:147.8.xxx.xxx 未訂閱
procedure TSyncLDAPThread.Execute; begin   CoInitialize(nil); while not Terminated do begin ScanSyncIf; Beep; Sleep(2000); end; CoUninitialize; end; http://pywong.hk.st http://www.lazybones.ca
samuelliang
一般會員


發表:2
回覆:5
積分:1
註冊:2005-02-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-02-25 11:27:07 IP:211.20.xxx.xxx 未訂閱
引言: procedure TSyncLDAPThread.Execute; begin CoInitialize(nil); while not Terminated do begin ScanSyncIf; Beep; Sleep(2000); end; CoUninitialize; end; http://pywong.hk.st http://www.lazybones.ca
感謝william的回覆,但是小弟還是有兩個問題 一、我要uses哪個unit呢? 二、我查了一下CoInitialize的說明文件(如下): The CoInitialize function initializes the Component Object Model(COM) library. You must initialize the library before you can call its functions. 大意是說呼叫此方法來初始化COM程式庫,而且你必須要在呼叫COM函數之前初始化。但我不懂得是這是寫NT Service時,若是與呼叫COM(我知道的是ADO確實是COM元件)有關係的話都必須如此寫嗎?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-02-25 11:38:15 IP:147.8.xxx.xxx 未訂閱
1. ActiveX 2. Yes, if you want to use COM in a thread, you need to initialize in your thread's context. ComObj does this for the MAIN thread only in its initialization/finalization sections.    http://pywong.hk.st http://www.lazybones.ca
samuelliang
一般會員


發表:2
回覆:5
積分:1
註冊:2005-02-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-02-25 11:46:56 IP:211.20.xxx.xxx 未訂閱
引言: 1. ActiveX 2. Yes, if you want to use COM in a thread, you need to initialize in your thread's context. ComObj does this for the MAIN thread only in its initialization/finalization sections. http://pywong.hk.st http://www.lazybones.ca
Dear william >ComObj does this for the MAIN thread only in its >initialization/finalization sections. 這句話我不懂耶!可不可以給個sample啊!難道我也要uses ComObj進來嗎? 還有我剛試了一下,還是沒有進到ScanSyncIf這個procedure裡面ㄋㄟ?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-02-25 12:03:18 IP:147.8.xxx.xxx 未訂閱
Most people don't call CoInitialize manually because the unit ComObj do this automatically. If you want to use COM in a thread, call it yourself. NT services need to be set to granted rights to interact with desktop if you want to use GUI. http://pywong.hk.st http://www.lazybones.ca
samuelliang
一般會員


發表:2
回覆:5
積分:1
註冊:2005-02-21

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-02-25 13:14:34 IP:211.20.xxx.xxx 未訂閱
引言: Most people don't call CoInitialize manually because the unit ComObj do this automatically. If you want to use COM in a thread, call it yourself. NT services need to be set to granted rights to interact with desktop if you want to use GUI. http://pywong.hk.st http://www.lazybones.ca
Hi, william: Thanks your quick response and answers. I will try it, if any result return to you.
samuelliang
一般會員


發表:2
回覆:5
積分:1
註冊:2005-02-21

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-02-25 13:54:39 IP:211.20.xxx.xxx 未訂閱
引言:
引言: Most people don't call CoInitialize manually because the unit ComObj do this automatically. If you want to use COM in a thread, call it yourself. NT services need to be set to granted rights to interact with desktop if you want to use GUI. http://pywong.hk.st http://www.lazybones.ca
Hi, william: Thanks your quick response and answers. I will try it, if any result return to you.
Hi, william: 3Q very much! 我初步已經可以select table了,由於我是在TThread.Execute這個主體內呼叫自訂的procedure,且該procedure會對資料庫做存取。因此,會在此procedure內呼叫到ADO元件,故CoInitialize必須寫在此procedure內。這是我的體會不知正確否?! Anyway, 還是感謝你的指點,Delphi真好玩,但是還是有一些'眉角'需要高人指點!
系統時間:2024-05-18 4:19:02
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!