编写NT服务程序遇到的麻烦! ( 积分: 100 )

  • 主题发起人 主题发起人 boyjason
  • 开始时间 开始时间
B

boyjason

Unregistered / Unconfirmed
GUEST, unregistred user!
编写了一个NT服务程序,想该服务根据系统时间判断是否从oracle数据库中导入到Sql Server。服务程序代码如下
unit Unit_main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr,Dialogs,
DB, ADODB,iniFiles,Forms,Unit_Import;
type
TImportDataSVC = class(TService)
Adoconn: TADOConnection;
ADOStoredProc: TADOStoredProc;
procedure ServiceContinue(Sender: TService;
var Continued: Boolean);
procedure ServiceExecute(Sender: TService);
procedure ServicePause(Sender: TService;
var Paused: Boolean);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService;
var Started: Boolean);
procedure ServiceStop(Sender: TService;
var Stopped: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController;
override;
{ Public declarations }
end;

var
ImportDataSVC: TImportDataSVC;
Importthread: TImport;
implementation
uses FunCrypt;
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
ImportDataSVC.Controller(CtrlCode);
end;
function TImportDataSVC.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TImportDataSVC.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
while not Terminateddo
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServiceExecute(Sender: TService);
begin
while not Terminateddo
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServicePause(Sender: TService;
var Paused: Boolean);
begin
Paused := True;
end;
procedure TImportDataSVC.ServiceShutdown(Sender: TService);
begin
Status := csStopped;
ReportStatus();
end;
procedure TImportDataSVC.ServiceStart(Sender: TService;
var Started: Boolean);
var
MyIniFile:TInifile;
Pwd,DbConStr,MS_SQLSERVER,MS_SQLUsr,MS_SQLPwd:String;
OracleParaList:TStringList;
begin
//服务启动时,将数据库参数获得
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Dbsetup.ini');
OracleParaList:=TStringList.Create;
MS_SQLSERVER:=Myinifile.ReadString('MS-SQL', 'Server', '');
MS_SQLUsr:=Myinifile.ReadString('MS-SQL', 'User', '');
MS_SQLPwd:=Myinifile.ReadString('MS-SQL', 'Password', '');
MS_SQLPwd:=_Crypt(MS_SQLPwd,65535,false);
DbConStr:=
'Provider=SQLOLEDB.1;Persist Security Info=True;'
+'User ID='+MS_SQLUsr+';Initial Catalog=GWBNMember;Data Source='+MS_SQLSERVER
+';Password='+MS_SQLPwd;
OracleParaList.Add(Myinifile.ReadString('Oracle', 'Server', ''));
OracleParaList.Add(Myinifile.ReadString('Oracle', 'User', ''));
Pwd:=Myinifile.ReadString('Oracle', 'Password', '');
OracleParaList.Add(_Crypt(Pwd,65535,false));
//解密密码
Adoconn.ConnectionString:=Dbconstr;
Adoconn.Connected:=True;
//此处无法建立线程
[red] Importthread:=TImport.Create(false,Adoconn,OracleParaList)[/red]
Started:= True;
end;

procedure TImportDataSVC.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Stopped := True;
end;
end.

此处为线程代码
unit Unit_Import;
interface
uses
inifiles,Classes,Sysutils,DB, ADODB,forms;
type
TImport = class(TThread)
private
FAdoConn:TAdoconnection;
FAdoStoredProc:TAdoStoredProc;
{ Private declarations }
protected
procedure Execute;
override;
constructor Create(Suspended:Boolean;
Adoconn:TAdoconnection;OracleParaList:TStringList);
end;
implementation
{ Import }
constructor TImport.Create(Suspended:Boolean;
Adoconn:TAdoconnection;OracleParaList:TStringList);
begin
inherited Create(Suspended);
FAdoConn:=Adoconn;
FAdoStoredProc.Connection:=FAdoConn;
FreeOnTerminate:=False;
end;

procedure TImport.Execute;
var
Hour,Min,Sec,MSec: Word;
TimeStamp:String;
MyInifile:Tinifile;
begin
{ Place thread code here }
{每次数据导入执行时间}
FreeOnTerminate:=false;
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Options.ini');
TimeStamp:=Myinifile.ReadString('Option', 'ImportTime', '00:00:00');
while not Terminateddo
begin
DecodeTime(Time,Hour,Min,Sec,MSec);
if Trim(TimeStamp)=Format('%-2.2d:%-2.2d',[Hour,Min]) then
//如果到导入数据时间
begin
{导入数据代码}
Sleep(60000);
end;
end;
MyInifile.free;
end;
end.

在上述红色加粗处无法建立线程,提示参数过多!
我是第一次写服务程序,对线程也只是一知半解,所以希望高手能帮我看看!
 
编写了一个NT服务程序,想该服务根据系统时间判断是否从oracle数据库中导入到Sql Server。服务程序代码如下
unit Unit_main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr,Dialogs,
DB, ADODB,iniFiles,Forms,Unit_Import;
type
TImportDataSVC = class(TService)
Adoconn: TADOConnection;
ADOStoredProc: TADOStoredProc;
procedure ServiceContinue(Sender: TService;
var Continued: Boolean);
procedure ServiceExecute(Sender: TService);
procedure ServicePause(Sender: TService;
var Paused: Boolean);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService;
var Started: Boolean);
procedure ServiceStop(Sender: TService;
var Stopped: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController;
override;
{ Public declarations }
end;

var
ImportDataSVC: TImportDataSVC;
Importthread: TImport;
implementation
uses FunCrypt;
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
ImportDataSVC.Controller(CtrlCode);
end;
function TImportDataSVC.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TImportDataSVC.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
while not Terminateddo
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServiceExecute(Sender: TService);
begin
while not Terminateddo
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServicePause(Sender: TService;
var Paused: Boolean);
begin
Paused := True;
end;
procedure TImportDataSVC.ServiceShutdown(Sender: TService);
begin
Status := csStopped;
ReportStatus();
end;
procedure TImportDataSVC.ServiceStart(Sender: TService;
var Started: Boolean);
var
MyIniFile:TInifile;
Pwd,DbConStr,MS_SQLSERVER,MS_SQLUsr,MS_SQLPwd:String;
OracleParaList:TStringList;
begin
//服务启动时,将数据库参数获得
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Dbsetup.ini');
OracleParaList:=TStringList.Create;
MS_SQLSERVER:=Myinifile.ReadString('MS-SQL', 'Server', '');
MS_SQLUsr:=Myinifile.ReadString('MS-SQL', 'User', '');
MS_SQLPwd:=Myinifile.ReadString('MS-SQL', 'Password', '');
MS_SQLPwd:=_Crypt(MS_SQLPwd,65535,false);
DbConStr:=
'Provider=SQLOLEDB.1;Persist Security Info=True;'
+'User ID='+MS_SQLUsr+';Initial Catalog=GWBNMember;Data Source='+MS_SQLSERVER
+';Password='+MS_SQLPwd;
OracleParaList.Add(Myinifile.ReadString('Oracle', 'Server', ''));
OracleParaList.Add(Myinifile.ReadString('Oracle', 'User', ''));
Pwd:=Myinifile.ReadString('Oracle', 'Password', '');
OracleParaList.Add(_Crypt(Pwd,65535,false));
//解密密码
Adoconn.ConnectionString:=Dbconstr;
Adoconn.Connected:=True;
//此处无法建立线程
[red] Importthread:=TImport.Create(false,Adoconn,OracleParaList)[/red]
Started:= True;
end;

procedure TImportDataSVC.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Stopped := True;
end;
end.

此处为线程代码
unit Unit_Import;
interface
uses
inifiles,Classes,Sysutils,DB, ADODB,forms;
type
TImport = class(TThread)
private
FAdoConn:TAdoconnection;
FAdoStoredProc:TAdoStoredProc;
{ Private declarations }
protected
procedure Execute;
override;
constructor Create(Suspended:Boolean;
Adoconn:TAdoconnection;OracleParaList:TStringList);
end;
implementation
{ Import }
constructor TImport.Create(Suspended:Boolean;
Adoconn:TAdoconnection;OracleParaList:TStringList);
begin
inherited Create(Suspended);
FAdoConn:=Adoconn;
FAdoStoredProc.Connection:=FAdoConn;
FreeOnTerminate:=False;
end;

procedure TImport.Execute;
var
Hour,Min,Sec,MSec: Word;
TimeStamp:String;
MyInifile:Tinifile;
begin
{ Place thread code here }
{每次数据导入执行时间}
FreeOnTerminate:=false;
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Options.ini');
TimeStamp:=Myinifile.ReadString('Option', 'ImportTime', '00:00:00');
while not Terminateddo
begin
DecodeTime(Time,Hour,Min,Sec,MSec);
if Trim(TimeStamp)=Format('%-2.2d:%-2.2d',[Hour,Min]) then
//如果到导入数据时间
begin
{导入数据代码}
Sleep(60000);
end;
end;
MyInifile.free;
end;
end.

在上述红色加粗处无法建立线程,提示参数过多!
我是第一次写服务程序,对线程也只是一知半解,所以希望高手能帮我看看!
 
unit Unit_main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr,Dialogs,
DB, ADODB,iniFiles,Forms,Unit_Import;
type
TImportDataSVC = class(TService)
Adoconn: TADOConnection;
ADOStoredProc: TADOStoredProc;
procedure ServiceContinue(Sender: TService;
var Continued: Boolean);
procedure ServiceExecute(Sender: TService);
procedure ServicePause(Sender: TService;
var Paused: Boolean);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService;
var Started: Boolean);
procedure ServiceStop(Sender: TService;
var Stopped: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController;
override;
{ Public declarations }
end;

var
ImportDataSVC: TImportDataSVC;
Importthread: TImport;
implementation
uses FunCrypt;
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord);
stdcall;
begin
ImportDataSVC.Controller(CtrlCode);
end;
function TImportDataSVC.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TImportDataSVC.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
while not Terminateddo
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServiceExecute(Sender: TService);
begin
while not Terminateddo
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServicePause(Sender: TService;
var Paused: Boolean);
begin
Paused := True;
end;
procedure TImportDataSVC.ServiceShutdown(Sender: TService);
begin
Status := csStopped;
ReportStatus();
end;
procedure TImportDataSVC.ServiceStart(Sender: TService;
var Started: Boolean);
var
MyIniFile:TInifile;
Pwd,DbConStr,MS_SQLSERVER,MS_SQLUsr,MS_SQLPwd:String;
OracleParaList:TStringList;
begin
//服务启动时,将数据库参数获得
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Dbsetup.ini');
OracleParaList.Create;
MS_SQLSERVER:=Myinifile.ReadString('MS-SQL', 'Server', '');
MS_SQLUsr:=Myinifile.ReadString('MS-SQL', 'User', '');
MS_SQLPwd:=Myinifile.ReadString('MS-SQL', 'Password', '');
MS_SQLPwd:=_Crypt(MS_SQLPwd,65535,false);
DbConStr:=
'Provider=SQLOLEDB.1;Persist Security Info=True;'
+'User ID='+MS_SQLUsr+';Initial Catalog=GWBNMember;Data Source='+MS_SQLSERVER
+';Password='+MS_SQLPwd;
OracleParaList.Add(Myinifile.ReadString('Oracle', 'Server', ''));
OracleParaList.Add(Myinifile.ReadString('Oracle', 'User', ''));
Pwd:=Myinifile.ReadString('Oracle', 'Password', '');
OracleParaList.Add(_Crypt(Pwd,65535,false));
//解密密码
Adoconn.ConnectionString:=Dbconstr;
Adoconn.Connected:=True;
//此处无法建立线程
[red] Importthread:=TImport.Create(false,Adoconn,OracleParaList)[/red]
Started:= True;
end;

procedure TImportDataSVC.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Stopped := True;
end;
end.

此处为线程代码
unit Unit_Import;
interface
uses
inifiles,Classes,Sysutils,DB, ADODB,forms;
type
TImport = class(TThread)
private
FAdoConn:TAdoconnection;
FAdoStoredProc:TAdoStoredProc;
{ Private declarations }
protected
procedure Execute;
override;
public //此处为我加的,你试试看行不行。
constructor Create(Suspended:Boolean;
Adoconn:TAdoconnection;OracleParaList:TStringList);
end;
implementation
{ Import }
constructor TImport.Create(Suspended:Boolean;
Adoconn:TAdoconnection;OracleParaList:TStringList);
begin
inherited Create(Suspended);
FAdoConn:=Adoconn;
FAdoStoredProc.Connection:=FAdoConn;
FreeOnTerminate:=False;
end;

procedure TImport.Execute;
var
Hour,Min,Sec,MSec: Word;
TimeStamp:String;
MyInifile:Tinifile;
begin
{ Place thread code here }
{每次数据导入执行时间}
FreeOnTerminate:=false;
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Options.ini');
TimeStamp:=Myinifile.ReadString('Option', 'ImportTime', '00:00:00');
while not Terminateddo
begin
DecodeTime(Time,Hour,Min,Sec,MSec);
if Trim(TimeStamp)=Format('%-2.2d:%-2.2d',[Hour,Min]) then
//如果到导入数据时间
begin
{导入数据代码}
Sleep(60000);
end;
end;
MyInifile.free;
end;
end.
 
楼上的仁兄,照你说的办法我改了一下,已经能够创建对象了,但又遇到了新的问题,在线程的Create事件中,执行到FAdoStoredProc.Connection:=FAdoConn;这一步就报错,不知是何原因!
 
呵呵,我的疏忽,Fadostoredproc这个对象都还没有建立,我就开始改它的属性了,应该在这之前加一句FAdoStoredProc:=Tadostoredproc.create(nil);
 
那搞定没有呀。呵呵,搞定就结貼吧
 
接受答案了.
 
后退
顶部