Cannot open database requested by the login - "Error Number:4060,State:1,Class:11"

Juan Jose Herrera 0 Reputation points
2024-09-13T18:52:54.5366667+00:00

I have an ASP.NET app, and when I launch the app and try to list items from a table by accessing the corresponding route, I get this error: SqlException: Cannot open database "appointments" requested by the login. The login failed. Login failed for user 'DESKTOP-KRBN07G\juan_'.

The database exists, and so do all the tables. I can connect to the database from SSMS and the 'Server Explorer' view in Visual Studio. I am using the connection string provided by the Server Explorer in Visual Studio. I also did a clean installation of Windows, Visual Studio, SQL Server, and SSMS. Currently, I have only one DB server and one database.
sql server win

My VS and connection string:
VS win

And finally the error:

error win

Please help me; this error is driving me mad. If you need any extra info, please let me know.

EDIT 1: I forgot to mention that I disabled my Windows firewall.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,822 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,316 Reputation points
    2024-09-13T21:08:24+00:00

    you need to give the user "DESKTOP-KRBN07G\juan_" access to the database. I assume you are running VS as admin, which gives you back door access.


  2. Juan Jose Herrera 0 Reputation points
    2024-09-14T21:39:36.9466667+00:00

    Well, guys, I found the problem, it was a wrong and 'hidden' Connection String.

    I created a new database with a different name, but the error in the browser after launching the app in my localhost kept pointing to the old database name.

    After that, I searched for the old database name throughout the project and discovered that my colleague had added a partial class inside the DataAccess project of the ASP.NET application, which runs in development mode and contains his connection string.

    I changed that, and now everything is working fine. All I knew was that the connection string should be in Program.cs or appsettings.json, and no other places.

    Thank you all for your time..¡


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.