用户“"登录失败,并且无法打开数据库"Database1.mdf";登录要求.登录失败.用户'rBcollo-PC \ rBcollo'的登录失败 [英] Login failed for user '' and Cannot open database "Database1.mdf" requested by the login. The login failed. Login failed for user 'rBcollo-PC\rBcollo'

查看:217
本文介绍了用户“"登录失败,并且无法打开数据库"Database1.mdf";登录要求.登录失败.用户'rBcollo-PC \ rBcollo'的登录失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以..我几乎解决了所有问题.但是现在我要处理另一个问题. 我使用了这个连接字符串:

So..I managed to almost finish all my problems.But now i deal with another one. I used this connectionstring :

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database=Database1.mdf");

它会引发下一个错误:

用户'

如果我删除 .mdf 扩展名,则会引发相同的错误

If i remove the .mdf extension it throws the same error

现在,如果我将以下内容添加到字符串中:

Now if i'll add the following to the string:

Integrated Security = true

它抛出了这个:

无法打开登录请求的数据库"Database1.mdf".登录失败. 用户'rBcollo-PC \ rBcollo'登录失败.

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

P.S for Integrated Security =否,它将引发用户登录失败"

P.S for Integrated Security = false it throws the "Login failed for user'"

问题是我没有为数据库使用任何用户名或密码.

The problem is that i'm not using any username or password for the Database.

需要帮助吗?

推荐答案

注意:如果将EntityFramework CoreSQL Server

此错误可能是由于原因Database does not exist和应用程序尝试执行数据库操作.

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(); 

什么是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.

这篇关于用户“"登录失败,并且无法打开数据库"Database1.mdf";登录要求.登录失败.用户'rBcollo-PC \ rBcollo'的登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆