accessing database from a windows-service

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

accessing database from a windows-service

Post by formi » Thu 14 Jun 2012 15:07

Hi

i've written a small programm using MS SQLserver and it works. If I use the code in a service, the service can not be started. When i try to open the connection it hangs. Have I to do something special to connect from a service? Thanks, Peter

AndreyZ

Re: accessing database from a windows-service

Post by AndreyZ » Fri 15 Jun 2012 09:44

Hello,

I cannot reproduce the problem. Please try creating a small sample to demonstrate the problem and send it to andreyz*devart*com . Also please specify the following:
- the exact version of UniDAC. You can learn it from the About sheet of TUniConnection Editor;
- the exact version of your IDE;
- the exact version of SQL Server and client. You can learn it from the Info sheet of TUniConnection Editor.

formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Re: accessing database from a windows-service

Post by formi » Fri 15 Jun 2012 13:41

Hi Andrey

thanks for your answer but when I wrote the example for you I found the problem myself. It seems to be necessary to call CoInitialize(nil) in the OnStartService-event. Now It works. Here the code:

uses ActiveX;

procedure TTestService1.ServiceStart(Sender: TService; var Started: Boolean);
begin
CoInitialize(nil); // <-- manually call CoInitialize()
UniConnection1.Connect;
Started := true;
end;

Thanks, Peter

AndreyZ

Re: accessing database from a windows-service

Post by AndreyZ » Fri 15 Jun 2012 15:51

Yes, when you work with SQL Server in a service, you should call the CoInitialize and CoUnInitialize functions in the ServiceExecute method. Here is an example:

Code: Select all

procedure TMyServ.ServiceExecute(Sender: TService);
begin
  CoInitialize(nil);

  // your code

  CoUnInitialize;
end;

Post Reply