简体   繁体   中英

Can not open database from odbc connection(using dsn) using windows service?

I am creating windows service which will connect to database using ODBC DSN, not using any username/password. Windows service is set as LocalService. (And I tried changing it to Netwrok Service as well as LocalSystem) I am using ODBC DSN as I will have different types of database (Sqlite,Sql,MySQl etc.). At moment I am getting below error:

ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "EmployeeDb" requested by the login. The login failed.
ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "EmployeeDb" requested by the login. The login failed.
StackTrace:    at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
   at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle)
   at System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection outerConnection, OdbcConnectionString connectionOptions)
   at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.Odbc.OdbcConnection.Open()

Here is my code for connect odbc using dsn name

 var conn = new OdbcConnection(@"DSN=Employee");
 conn.Open();
 conn.Close();

not using any username/password. Windows service is set as LocalService.

If you aren't using a username / password, then you're presumably using the account identity for authentication, ie Windows Authentication. For this to work, the service needs to be running in an account that has access to the database. LocalService and LocalSystem will never work: by definition, those accounts are local. NetworkService might work, if you've added the machine account (for the machine's network identity) to the database server with permission for that database; however, the usual approach here is to tell the service to run in a specific domain account (created as a service account, with the "log on as a service" privilege), and make sure that account is defined on the database server and has permission to the database being requested.

To investigate: I'm assuming you have access to the database; so temporarily change the service to run as you , and see if it works (assuming you have the "log on as a service" permission). If it does, then that is definitely the problem - so go ahead and configure the actual account that you want it to run as.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM