Cannot Open Database "Test" Requested by the Login. the Login Failed. Login Failed for User 'Xyz\Aspnet'

Cannot open database test requested by the login. The login failed. Login failed for user 'xyz\ASPNET'

Well, the error is pretty clear, no? You are trying to connect to your SQL Server with user "xyz/ASPNET" - that's the account your ASP.NET app is running under.

This account is not allowed to connect to SQL Server - either create a login on SQL Server for that account, or then specify another valid SQL Server account in your connection string.

Can you show us your connection string (by updating your original question)?

UPDATE: Ok, you're using integrated Windows authentication --> you need to create a SQL Server login for "xyz\ASPNET" on your SQL Server - or change your connection string to something like:

connectionString="Server=.\SQLExpress;Database=IFItest;User ID=xyz;pwd=top$secret"

If you have a user "xyz" with a password of "top$secret" in your database.

Cannot open database Identity requested by the login. The login failed. Login failed for user 'DESKTOP\User'

Your application is using a "Trusted connection" to the SQL Server database. So it is trying to login with the current user of the computer.

You need to go into SQL Server Object Explorer or SQL Server Management Studio and add 'DESKTOP\User' as a login to SQL Server and a user in your database.

Here are the steps

https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/create-a-database-user?view=sql-server-ver15#SSMSProcedure

Screenshot of the add user page

https://serverfault.com/questions/366492/how-can-i-add-myself-to-my-local-sql-servers-windows-authentication

Make sure to go into the "User Mapping" tab and tick the database you want to use. If this is development you can tick "db_owner" so you have all permissions.

Cannot open database MyDatabaseName requested by the login. The login failed. Login failed for user 'IIS APPPOOL\MyAppName

You are using SSPI Integrated Security, thus it is trying to use Windows authentication. Try setting to False:

 connectionString="Data Source=HAPPY\SQLEXPRESS;uid=sa;password=123;Initial Catalog=dwm;integrated security=False;" providerName="System.Data.SqlClient" 

Login failed for user '' and Cannot open database Database1.mdf requested by the login. The login failed. Login failed for user 'rBcollo-PC\rBcollo'

Note: My answer would be applicable if you are using EntityFramework Core with SQL Server

This error could be because of the reason 'Database does not exist' and application try to perform DB operations.

All you need to use the following line before performing any database operation.

_context.Database.EnsureCreated(); 

And what is EnsureCreated?

    // Summary:
// Ensures that the database for the context exists. If it exists, no action is
// taken. If it does not exist then the database and all its schema are created.
// If the database exists, then no effort is made to ensure it is compatible with
// the model for this context.
// Note that this API does not use migrations to create the database. In addition,
// the database that is created cannot be later updated using migrations. If you
// are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate()
// method to ensure the database is created and all migrations are applied.
//
// Returns:
// True if the database is created, false if it already existed.

How to create connection with SQL Server in Visual Studio? Getting SqlException: Login failed for user

Let's clarify this part, somehow you cannot access to the database engine.

The SQL Server supports two authentication modes:

  • Windows Authentication
  • SQL Server Authentication

First, you tried to used Windows Authentication, and then SQL Server Authentication.

In the connection string Trusted_Connection=True or Integrated Security=true means that connection will use Windows Authentication. So, if there is one of these two parameters then you don't need to specify User and Password.

To check your server name connect to the SQL Engine through SQL Server Management Studio, and execute the following query:

SELECT @@SERVERNAME

As a result, I got DESKTOP-29GJ2IJ because this is a local machine and there are no named instances like for example DESKTOP-29GJ2IJ\MSSQLSERVER2017 I can use the name localhost in the connection string, but let's use the value from the result of the query.

Once, I know the exact name of the server then I can write a connection string:

Server=DESKTOP-29GJ2IJ;Database=DevShop;Trusted_Connection=True;

Useful page for connection strings is
https://www.connectionstrings.com/sql-server/

You can connect to the another databases, so it is not only related to the MSSQL Server.



Related Topics



Leave a reply



Submit