Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi why
CDatabase
can't open in Window Service in runtime?(it's VC++ window service)

CString sConnectionString = L"ODBC;DSN=MyDB;Trusted_Connection=Yes";
CDatabase db;
db.SetLoginTimeout(8000);
db.SetQueryTimeout(8000);
db.Open( NULL, 0, 0, sConnectionString );


If this exe , runs as a console app, the Database opens right . But when it runs as a window service in service Manager, it cann't open Database!!!
please help me!

What I have tried:

why winodw service cann't open Database? (CDatabase.Open())
Posted
Updated 9-Oct-18 4:10am
Comments
MadMyche 9-Oct-18 10:15am    
Is there an error when it attempts to connect? When you are using a Trusted_Connection you need to know what credentials the "service" version is using
Richard Deeming 9-Oct-18 11:33am    
Is "MyDB" set up as a "User DSN", or a "System DSN"?
Zon-cpp 10-Oct-18 6:55am    
it is as a User DSN
I think my windows has problem to set an "account logon" for services. I can't set an account for my service. It has "Access denied" error!
Richard Deeming 10-Oct-18 13:55pm    
If it's a user DSN, then it's only available to the account in which you set it up. Unless the service is running under your account, it won't be able to access the DSN.

Try setting it up as a system DSN instead.

1 solution

It's probably because you're using a trusted connection in your connection string.

Using a Trusted Connection means that the user credentials presented to the database to login are the ones the code is running under. In the case where your database code works, the code is running as YOU.

In the case of the Windows service not opening the database, chances are really good you're running the code under the Local System account, which will NOT have access permissions to your database.

Get rid of the Trusted Connection part of your connection string and replace it with a username and password of an account that does have appropriate permissions to login to that database.

You'll probably have to create an account on the SQL Server specifically for this database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900