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
accessing database from a windows-service
-
AndreyZ
Re: accessing database from a windows-service
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.
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.
Re: accessing database from a windows-service
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
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
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;