Search code examples
c#sqlsql-serversql-server-2008-r2backgroundworker

Cannot open database "Inventory_New" requested by the login. The login failed. Login failed for user 'sa'


I have made my database in SQL Server 2008 R2 Express Edition and the software is made in VS C# Windows Form and is used to store data of inventory of a big company.

I use a BackgroundWorker in the software to continuously check if the stock has been updated or not but for some reason it shows the below error.

Cannot open database "Inventory_New" requested by the login. The login failed. Login failed for user 'sa'.

I have already gone through many blogs and article but none of the solution works for me.

I can login with Sql Server Management Studio with the user sa and it is responding well.

The software works just fine but throughout the day it randomly shows the above error (6-7 time a day no fixed time)

SQL Error Log

Below is the error i found in SQL Error Log but i do not understand what that means

2017-05-12 05:01:41.78 Logon Error: 18456, Severity: 14, State: 38.
2017-05-12 05:01:41.78 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database. [CLIENT: ]

Edit

Lately i saw that when i got this error the database for some reason went in recovery mode.

I also checked some articles for why the Database goes in recovery mode but none of them seems to fit the bill because in my case the database comes online automatically after 2-3 seconds of short break.


Solution

  • The error indicates you the database, Inventory_New, that can't be open. This means that your application asks for this database as the initial catalog, but the database is not online. It can be in the middle of restore, for example, or just be offline. When you connect to server using Management Studio, you don't submit that database, I guess the default database for sa login is master, so you logged in successfully. You should check why your Inventory_New database is not online