gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:06:59 24 4
gpt4 key购买 nike

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

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

然后它抛出下一个错误:

Login failed for user '

如果我删除 .mdf 扩展,它会抛出同样的错误

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

Integrated Security = true

它抛出这个:

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

P.S for Integrated Security = false 它抛出“用户登录失败”

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

有什么帮助吗?

最佳答案

注意:如果您将 EntityFramework CoreSQL Server 一起使用,我的回答将适用

此错误可能是由于“数据库不存在”和应用程序尝试执行数据库操作的原因造成的。

在执行任何数据库操作之前,您需要使用以下行。

_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.

关于c# - 用户 '' 登录失败,无法打开登录请求的数据库 "Database1.mdf"。登录失败。用户 'rBcollo-PC\rBcollo' 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com