Windows Service unable to connect to SQL Server database, but can connect in Visual Studio

adnanabbas99j 1 Reputation point
2021-06-10T19:54:10.103+00:00

I created a Windows service in Visual Studio 2019. The service works just fine in Visual Studio, but when I deploy it to my local system, the service is now unable to connect to the SQL Server database.

I get this error:

Description: Service cannot be started. System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'GLOADMIN'.

at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)

I was initially using Windows authentication, before I changed to SQL Server authentication, both worked in Visual Studio but after deployment as a service on my local Windows 10 system, it fails.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,809 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,205 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 101.8K Reputation points MVP
    2021-06-10T21:32:55.077+00:00

    Look in the SQL Server errorlog. There you find more detailed information about why the login failed.

    You find the errorlog in C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\LOG\Errorlog. Note that the exact path depends on your SQL Server version and instance name.

    0 comments No comments

  2. Ronen Ariely 15,096 Reputation points
    2021-06-11T20:35:48.053+00:00

    Good day,

    Description: Service cannot be started. System.Data.SqlClient.SqlException (0x80131904): Login failed for user...
    I was initially using Windows authentication, before I changed to SQL Server authentication, both worked in Visual Studio but after deployment as a service on my local Windows 10 system, it fails.

    Note: It is recommended in most cases to use Windows Authentication if this is supported.

    More information that could help

    • is your SQL Server installed in the same machine as the service (the application that connect the server)?
    • what is your connection string (remove the sensitive information like password)
    • As other already asked, we need to get the exact full information from the error log file

    In the mean time let's play with some guessing, please check if this help you

    • Make sure that SQL Server is configured to use Mixed mode
    • Since you are using SQL Authentication now, in the connection string set: Integrated Security=False
    0 comments No comments