SQL Server login problem for a windows service on reboot
I have a service that has a dependency on SQL server express (2005). Every morning when I reboot my machine, I see that the service has not started. In the event viewer, I see the following error:
Service cannot be started. System.Data.SqlClient.SqlException: Cannot open database "MyDatabase" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\SYSTEM'.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectio开发者_运维问答nTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString conn...
If I now restart the service, it works fine but not on when I first power up the machine.
Any clues why this is happening?
JD.
I think it may happen when your service is started before SQL Server Express. I recommend checking out one of these solutions below:
- add more attempts to login to database with appropriate delay time
- check if Sql Server is running before you try to login to your database
You can use the "regedit" program to edit the dependency list for the service, this will cause the service to wait until the SQL Server Express service has started. Open the "regedit" program and navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<service name>
. Look for the key called "DependOnService" of type REG_MULTI_SZ
. If your server is Windows 2003 then the REG_MULTI_SZ
editor allows a string value per line otherwise the binary editor on Windows 2000 requires a null byte between each letter. You want to enter the display name of the SQL Server 2005 Express as shown in the services manager. A default install typically displays "SQL Server (SQLEXPRESS)". If you do it correctly when you view the "Dependencies" tab of you dependent service you should see SQL Server Express listed in the depends on section.
The another easiest solution is set your windows service - start as Automatic (delayed Start).
A service marked as Automatic (Delayed Start) will start shortly after all other services designated as Automatic have been started. In my experience, this means that they are started 1-2 minutes after the computer boots.
精彩评论