How to access remote database server from windows service
-
I have encountered a problem when i try to access database built on a remote server... but when i tried accessing database on my local computer, this problem did not occur... Anybody know how to solve this problem?
It would make this question much easier to answer if you could at least post the error you get when you try to access the database. But if I have to guess, I would think that you are using the default service account. That account doesn't have rights to access the network, instead you should create a user with normal user rights, and use that user with your service. Magnus
-
I have encountered a problem when i try to access database built on a remote server... but when i tried accessing database on my local computer, this problem did not occur... Anybody know how to solve this problem?
From your service running under the system accoun, you can impersonate a user who has permission to access the database: HANDLE token = NULL;j if (LogonUser(szUser, szHostName, szPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token)) { if (!ImpersonateLoggedOnUser(token)) { CloseHandle(token); token = NULL; } } and then to revert to the system account: if (token) { RevertToSelf(); CloseHandle(token); }
-
It would make this question much easier to answer if you could at least post the error you get when you try to access the database. But if I have to guess, I would think that you are using the default service account. That account doesn't have rights to access the network, instead you should create a user with normal user rights, and use that user with your service. Magnus
But i have used this account in many normal applications(not service) to access the database already, it works fine. Does that mean it should not be because the account does not have the rights to access the network? I will try to add in a normal account, and feedback here~ thanks so much~
-
From your service running under the system accoun, you can impersonate a user who has permission to access the database: HANDLE token = NULL;j if (LogonUser(szUser, szHostName, szPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &token)) { if (!ImpersonateLoggedOnUser(token)) { CloseHandle(token); token = NULL; } } and then to revert to the system account: if (token) { RevertToSelf(); CloseHandle(token); }
-
It would make this question much easier to answer if you could at least post the error you get when you try to access the database. But if I have to guess, I would think that you are using the default service account. That account doesn't have rights to access the network, instead you should create a user with normal user rights, and use that user with your service. Magnus