Cannot open database “MyDb” requested by the login. The login failed. Login failed for user 'My-PC\\MrD'

眉间皱痕 提交于 2019-12-12 01:15:33

问题


I have been connecting to my local database (SQL Express) fine all day from my C# console application.

I have made an update, where I create a database and table from within the C# code using ADO.NET

When I do this, my normal connection string fails, which is

    Data Source=.\\sqlexpress;  Trusted_Connection=True;  Initial Catalog=ThisIsMyDataBase;

The error message is

Cannot open database "ThisIsMyDataBase" requested by the login. The login failed.\r\nLogin failed for user 'My-PC\\MrD'.

If I open the tables in SSMS, then I can access everything, using Windows Authentication.

I was under the impression that the Trusted_Connection=True would mean I have permission, and that I'm accessing it with my Windows account (not an SQL account) would mean I don't see this issue, but I'm clearly wrong. I'm not sure how to solve this.


回答1:


The error itself is saying the user dont have access failed for user 'My-PC\\MrD'. do a login for the user from sql server itself




回答2:


You propably do not have the sql server login, which is associated with your WIN user, mapped to a database user.

Check that your model database (under System databases in management studio) has your WIN user listed under Security/Users. If not, add it. Then delete the MyDb database and create it again using you code. The CREATE DATABASE statement uses a copy of the model database to create the new database and its metadata.




回答3:


I have the solution but I don't understand it at all

The answer is to rename the database with less characters. My actual database was 27 characters long, I've just given it a 4 character long name (simply called My01) and it works fine.



来源:https://stackoverflow.com/questions/30212318/cannot-open-database-mydb-requested-by-the-login-the-login-failed-login-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!