Recent

Author Topic: ODBC Connection in a Windows service  (Read 2104 times)

relocate

  • New Member
  • *
  • Posts: 22
ODBC Connection in a Windows service
« on: September 21, 2021, 08:44:06 am »
I want to connect to an MSSQL Server via a service in Windows.
But it won't connect.
In a standard program everything works as it should, but not in the service.
The suggested solution to create a system DNS doesn't help.
The handles are created, only when it comes to the Connection it goes wrong.
I dont't use Lazarus only pure Free Pascal and the odbcsqldyn unit.
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: ODBC Connection in a Windows service
« Reply #1 on: September 21, 2021, 10:24:59 am »
Just thinking out loud - could this be a privilege issue because the service doesn't run within a user context? Can you retrieve an error code, this could help in identifying the problem.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: ODBC Connection in a Windows service
« Reply #2 on: September 21, 2021, 10:27:47 am »
1) is your service dependent on the Database-Service? As in: Is the DB-Service up and running, before your Service starts?
2) Have you tried within your service to Login with your Windows-user-Account?
https://www.codeproject.com/Questions/185651/ODBC-through-Windows-Service-Win-7-x64
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

relocate

  • New Member
  • *
  • Posts: 22
Re: ODBC Connection in a Windows service
« Reply #3 on: September 21, 2021, 11:27:04 am »
@ccrause
I only get -1 as errorcode. That's the returncode from SQLConnect, I don't know if i can get any other code.

@zvoni
1) No, the Database is located on an server which is allready running, so no dependences.
2) Yes, I allready tried it. But you brought me onto the right course, I took the wrong user. (local instead of domain user), it works, thank you!
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: ODBC Connection in a Windows service
« Reply #4 on: September 21, 2021, 12:11:12 pm »
Ah, yes.
Domain-User makes more sense, if the DB-Server is on another machine of the Network
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

relocate

  • New Member
  • *
  • Posts: 22
Re: ODBC Connection in a Windows service
« Reply #5 on: September 21, 2021, 12:28:20 pm »
Yes, I thought I'd took the right user, but NO.
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: ODBC Connection in a Windows service
« Reply #6 on: September 21, 2021, 12:52:31 pm »
try initialize multithreading for the COM objects:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ComObj;
  3.  
  4. begin
  5.   CoInitializeEx(nil, COINIT_MULTITHREADED);
  6.   SQLConnection1.Connect;
  7.   //queries
  8.   SQLConnection1.Disconnect;
  9.   CoUnInitialize;
  10. end;
Best regards / Pozdrawiam
paweld

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: ODBC Connection in a Windows service
« Reply #7 on: September 21, 2021, 02:40:52 pm »
Pawel
what's that got to do with the initial problem?
The OP has a Network-Service which wasn't running under a User-Context trying to access a network-ressource which is bound to fail
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

relocate

  • New Member
  • *
  • Posts: 22
Re: ODBC Connection in a Windows service
« Reply #8 on: September 21, 2021, 03:10:19 pm »
@paweld
according to zvoni I would ask the same, on which circumstances I should use this for the problem.
Or did the answer just go to the wrong question?
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: ODBC Connection in a Windows service
« Reply #9 on: September 21, 2021, 05:16:01 pm »
Admittedly I am not using ODBC but SDAC components, but with windows service the component does not want to connect to MSSQL until multithreading is initiated for COM objects.
Best regards / Pozdrawiam
paweld

relocate

  • New Member
  • *
  • Posts: 22
Re: ODBC Connection in a Windows service
« Reply #10 on: September 22, 2021, 09:04:47 am »
Thanks for the clarification, I don't know if there is a correlation, but with the solution through the windows service it works. And it seems to be a valued solution because in some cases services need user rights to forefill their tasks.
OS, Lazarus, FPC: Win (no Lazarus, FPC 2.4.4) | CPU-Target: 32Bit

 

TinyPortal © 2005-2018