Programming => Databases => Topic started by: relocate on September 21, 2021, 08:44:06 am
Title: ODBC Connection in a Windows service
Post by: relocate 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.
Title: Re: ODBC Connection in a Windows service
Post by: ccrause 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.
Title: Re: ODBC Connection in a Windows service
Post by: Zvoni 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
Title: Re: ODBC Connection in a Windows service
Post by: relocate 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!
Title: Re: ODBC Connection in a Windows service
Post by: Zvoni 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
Title: Re: ODBC Connection in a Windows service
Post by: relocate on September 21, 2021, 12:28:20 pm
Yes, I thought I'd took the right user, but NO.
Title: Re: ODBC Connection in a Windows service
Post by: paweld on September 21, 2021, 12:52:31 pm
try initialize multithreading for the COM objects:
Title: Re: ODBC Connection in a Windows service
Post by: Zvoni 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
Title: Re: ODBC Connection in a Windows service
Post by: relocate 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?
Title: Re: ODBC Connection in a Windows service
Post by: paweld 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.
Title: Re: ODBC Connection in a Windows service
Post by: relocate 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.