By coure2011


2010-04-04 21:11:00 8 Comments

I have created a web service which is saving some data into to db. But I am getting this error:

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

My connection string is

Data Source=.\SQLExpress;Initial Catalog=IFItest;Integrated Security=True

22 comments

@Krishanth Rajkumar 2019-09-06 07:46:06

In my case it is a different issue. The database turned into single user mode and a second connection to the database was showing this exception. To resolve this issue follow below steps.

  1. Make sure the object explorer is pointed to a system database like master.
  2. Execute a exec sp_who2 and find all the connections to database ‘my_db’. Kill all the connections by doing KILL { session id } where session id is the SPID listed by sp_who2.
USE MASTER;
EXEC sp_who2
  1. Alter the database
USE MASTER;
ALTER DATABASE [my_db] SET MULTI_USER
GO

@Greg Z. 2019-07-23 20:24:43

In my case, I was running a Windows Service under "System" identity. The error was:

System.Data.SqlClient.SqlException (0x80131904): 
Cannot open database "MyDbName" requested by the login. The login failed.
Login failed for user 'MYDOMAINNAME\HOSTNAME$'.

The problem is that the error is very misleading. Even after I added 'MYDOMAINNAME\HOSTNAME$' login to the database, and granted this login sysadmin access and added a user for that login on my target database, and made that user dbowner, I was still getting the same error. Apparently I needed to do the same for 'NT AUTHORITY\SYSTEM' login. After I did that, I was able to login without a problem. I don't know why the error message complains about 'MYDOMAINNAME\HOSTNAME$'. I deleted that login and the corresponding user and everything still works.

@AmirHossein Rezaei 2019-06-21 06:26:52

If you haven't created the database in your server you will get the same login error.Make sure that the database exist before you login.

@Jesse Hufstetler 2019-06-17 18:01:54

Inspired by cyptus's answer I used

_dbContext.Database.CreateIfNotExists();

on EF6 before the first database contact (before DB seeding).

@Koki Monoto 2018-07-26 16:12:33

NB: If using a windows service to host the webservice.

You have to insure that your webservice is using the right Log on account to connect to SQL Server.

  • Open services(I assume the windows service has been install)
  • Right click on the service and goto properties.
  • Click on "Log On" Tab
  • Click on "This account" radio button
  • Click "Browse"
  • Enter Pc user name in Text Field and click "Check Name" Button to the right.
  • Click on text in Text Field, press "OK" button
  • enter login password and Apply

@cyptus 2018-02-15 08:25:38

For me the database was not created and EF code first should have created it but always endet in this error. The same connection string was working in aspnet core default web project. The solution was to add

_dbContext.Database.EnsureCreated()

before the first database contact (before DB seeding).

@Grey Wolf 2017-12-20 07:14:54

It also happen when you type wrong name of DB

ex : xxx-db-dev to xxx-dev-db

Sometime, it's just a stupid mistake . I take about more than 1 hours to find out this :( because i just try alot of difficult thing first

@Chris Halcrow 2017-05-05 06:29:44

Even if you've set the login as DB owner and set the user mapping for the database that will use the login, check that the actual DB user (not just the login) has the role of 'owner'.

@Anton Lyhin 2017-04-27 21:04:45

The Issue

The error presents itself as a message similar to this:

Cannot open database "DATABASE NAME" requested by the login. The login failed. Login failed for user XYZ.

  • The error cannot usually be rectified by a simple Visual Studio or full-computer restart.
  • The error can also be found as a seemingly locked database file.

The Fix

The solution is laid in the following steps. You will not lose any data in your database and you should not delete your database file!

Pre-requisite: You must have installed SQL Server Management Studio (Full or Express)

  1. Open SQL Server Management Studio
  2. In the "Connect to Server" window (File->Connect object explorer) enter the following:
    • Server type : Database Engine
    • Server name : (localdb)\v11.0
    • Authentication : [Whatever you used when you created your local db. Probably Windows Authentication).
  3. Click "Connect"
  4. Expand the "Databases" folder in the Object Explorer (View->Object Explorer, F8)
  5. Find your database. It should be named as the full path to your database (.mdf) file
    • You should see it says "(Pending Recovery)" at the end of the database name or when you try to expand the database it won't be able to and may or may not give you an error message.
    • This the issue! Your database has crashed essentially..
  6. Right click on the database then select "Tasks -> Detach...".
  7. In the detach window, select your database in the list and check the column that says "Drop Connections"
  8. Click OK.
  9. You should see the database disappear from the list of databases. Your problem should now be fixed. Go and run your application that uses your localdb.
  10. After running your application, your database will re-appear in the list of databases - this is correct. It should not say "Pending recovery" any more since it should be working properly.

The source of the solution: https://www.codeproject.com/Tips/775607/How-to-fix-LocalDB-Requested-Login-failed

@Shimmy 2017-11-30 17:50:06

The problem is I can't even find that DB.

@Tomas Kubes 2016-12-03 21:00:38

In my case the asp.net application can usually connect to database without any problems. I noticed such message in logs. I turn on the SQL server logs and I find out this message:

2016-10-28 10:27:10.86 Logon       Login failed for user '****'. Reason: Failed to open the explicitly specified database '****'. [CLIENT: <local machine>]
2016-10-28 10:27:13.22 Server      SQL Server is terminating because of a system shutdown. This is an informational message only. No user action is required.

So it seems that server was restarting and that SQL server whad been shutting down a bit earlier then ASP.NET application and the database was not available for few seconds before server restart.

@marc_s 2010-04-04 21:16:42

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.

@wRAR 2010-04-05 18:27:51

Not "is not allowed to connect to SQL Server", but "is not allowed to use database "test"".

@marc_s 2010-04-05 18:33:02

@wRAR: true - but I guess the chance the user login exists on the server, but isn't enabled in this particular database, is probably slim (at least I would think)

@wRAR 2010-04-05 18:36:53

See also "Database=IFItest" vs "test", though that may be a misprint.

@Ron16 2017-09-28 18:55:51

One point you never touched upon is that the DB Name could be wrong too. I was getting thrown a ditto looking exception message from my Azure SQL Server, and turned out I had given wrong name for my DB in one of the connectionstrings in the C# code in my ASP.NET project.

@Ehsan 2016-06-28 14:34:13

I used Windows authentication to connect to local database .mdf file and my local server was sql server 2014. My problem solved using this connection string:

string sqlString = " Data Source = (LocalDB)\\MSSQLLocalDB;" + "AttachDbFilename = F:\\.........\\myDatabase.mdf; Integrated Security = True; Connect Timeout = 30";

@Anonymous 2016-09-26 03:31:57

I had to remove this part "User Instance=True" to make my local DB working

@Kamran 2016-03-15 18:16:50

This Works for me.

  1. Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties
  2. In newly opened screen of Login Properties, go to the “User Mapping” tab.
  3. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed.
  4. Click OK.

Read this blog.

http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/

@eFarzad 2019-12-05 09:19:21

thanks, using "NT AUTHORITY\SYSTEM" solved my problem.

@ParPar 2015-05-14 08:10:32

I had this problem and what solved it for me was to:

  • Go to the Application pools in the IIS
  • Right click on my project application pool
  • In Process Model section open Identity
  • Choose Custom account option
  • Enter your pc user name and password.

@Mazen el Senih 2019-09-10 12:13:28

Worked for me too!

@Justice O. 2015-03-26 19:58:54

Most times, it's not a login issue, but an issue with creating the database itself. So if there is an error creating your database, it would not be created in the first place. In which case if you tried to log in, regardless of the user, login would fail. This usually happens due to logical misinterpretation of the db context.

Visit the site in a browser and REALLY read those error logs, this can help you spot the problem with you code (usually conflicting logic problems with the model).

In my case, the code compiled fine, same login problem, while I was still downloading management studio, I went through the error log, fixed my db context constraints and site started running fine....meanwhile management studio is still downloading

@user3041948 2014-12-07 13:54:46

Some times this trouble may appear if you open this db in another sql server (as example, you launch sql managment studio(SMS) and add this db), and forget stop this server. As result - you app try to connect with user already connected in this db under another server. To fix that, try stop this server by Config. dispatcher sql server.

My apologies about bad english. Kind regards, Ignat.

@Rajeev 2011-06-24 12:16:57

The best solution for the login problem is to create a login user in sqlServer. Here are the steps to create a SQL Server login that uses Windows Authentication (SQL Server Management Studio):

  1. In SQL Server Management Studio, open Object Explorer and expand the folder of the server instance in which to create the new login.
  2. Right-click the Security folder, point to New, and then click Login.
  3. On the General page, enter the name of a Windows user in the Login name box.
  4. Select Windows Authentication.
  5. Click OK.

For example, if the user name is xyz\ASPNET, then enter this name into Login name Box.

Also you need to change the User mapping to allow access to the Database which you want to access.

@demongolem 2014-01-13 16:40:10

I have not seen this mentioned in the previous issues, so let me throw out another possibility. It could be that IFItest is not reachable or simply does not exist. For example, if one has a number of configurations, each with its own database, it could be that the database name was not changed to the correct one for the current configuration.

@shashank 2013-09-18 07:17:25

I tried to update the user, and it worked. See the command below.

USE ComparisonData// databaseName
EXEC  sp_change_users_login @Action='update_one', @UserNamePattern='ftool',@LoginName='ftool';

Just replace user('ftool') accordingly.

@DevDave 2012-10-26 15:22:45

I found that I also had to set the UserMapping option when creating a new login and this solved the problem for me. Hope that helps anyone that also found themselves stuck here!

Edit: Setting the login as db owner solved the next problem, too

@gbn 2010-04-05 10:21:57

  • Either: "xyz\ASPNET" is not a login (in sys.server_principals)
  • Or: "xyz\ASPNET" is set up but not mapped to a user in the database test (sys.database_principals)

I'd go for the 2nd option: the error message implies the default database is either not there or no rights in it, rather than not set up as a login.

To test if it's set up as a login

SELECT SUSER_ID('xyz\ASPNET') -- (**not** SUSER_SID)

If NULL

CREATE LOGIN [xyz\ASPNET] FROM WINDOWS

If not NULL

USE test
GO
SELECT USER_ID('xyz\ASPNET')

If NULL

USE test
GO
CREATE USER [xyz\ASPNET] FROM LOGIN [xyz\ASPNET]

@Mazen el Senih 2019-09-10 12:04:18

This is just great!, a simple workflow to detect and solve the problem.

@Giorgi 2010-04-04 21:24:09

The best option would be to use Windows integrated authentication as it is more secure than sql authentication. Create a new windows user in sql server with necessary permissions and change IIS user in the application pool security settings.

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Login failed for user 'DOMAIN\MACHINENAME$'

2 Answered Questions

4 Answered Questions

1 Answered Questions

Cannot open database test requested by the login. The login failed

  • 2017-01-30 08:30:38
  • akshay aswal
  • 392 View
  • 0 Score
  • 1 Answer
  • Tags:   asp.net iis

2 Answered Questions

[SOLVED] error "Login failed for user" C# with SQLConnection

  • 2014-01-20 15:56:53
  • SunEye
  • 8640 View
  • 0 Score
  • 2 Answer
  • Tags:   c#

1 Answered Questions

1 Answered Questions

Sponsored Content