Cannot open database “test” requested by the login

2019-01-01 09:15发布

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

18条回答
深知你不懂我心
2楼-- · 2019-01-01 10:02

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

查看更多
路过你的时光
3楼-- · 2019-01-01 10:03

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.

查看更多
回忆,回不去的记忆
4楼-- · 2019-01-01 10:05

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.
查看更多
步步皆殇っ
5楼-- · 2019-01-01 10:05

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.

查看更多
后来的你喜欢了谁
6楼-- · 2019-01-01 10:06
  • 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]
查看更多
柔情千种
7楼-- · 2019-01-01 10:06

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";
查看更多
登录 后发表回答