用户 '' 登录失败,无法打开登录所请求的数据库 "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'

所以..我几乎完成了所有 problems.But 现在我要处理另一个。 我使用了这个连接字符串:

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 对于 Integrated Security = false 它抛出 "Login failed for user'"

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

有什么帮助吗?

一旦您使用 integrated security = true,它就会使用您的 windows 凭据登录到 sql 实例。您必须授予此用户对 sql 实例的权限。如果您不指定 integrated security = true,那么它会以匿名用户身份登录,这就是用户'.'登录失败的原因。您可以使用第三个选项创建一个 sql 帐户,然后将它们传递到连接字符串中并登录。取决于你想做什么。

如果您设置 Integrated Security = true,那么系统将尝试使用您当前的 windows 凭据登录数据库。如果您没有特别授予数据库中当前用户的权限,那么这将不起作用。

如果您设置 Integrated Security = false 或根本没有 Integrated Security 选项,则您需要提供用户名和密码。

Data Source=.\SQLEXPRESS;Database=Database1.mdf;User Id=myUsername;
Password=myPassword;

你说你没有使用用户名或密码的说法没有任何意义。您必须拥有数据库的用户名和密码。

这没有得到回答,所以我想我会列出我的建议,似乎也出现在 VS Community 2015 中,运行作为管理员的 VS 似乎解决了这个问题。

尽管数据库是在 visual studio 运行ning 在我的帐户下创建的,并且 Web 服务在 运行ning 在我的帐户下,但似乎无法访问数据库没有管理员。

也许其他人可以回答为什么需要这样做,只知道很多 VS 功能正常工作需要管理员,不确定为什么一开始没有启用它。

仅使用本地开发数据库就非常烦人..应该可以正常工作,没有所有这些摆弄和错误信息。

如果你使用的是netcore,不要忘记在项目目录中运行 dotnet ef database update。我遇到了同样的错误并为我修复了它。

注意:如果您将 EntityFramework CoreSQL Server

一起使用,我的回答将适用

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

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

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

在ASP.NetCore中,如果还没有创建数据库,可能会出现类似的错误! (错误消息可能会让您感到困惑,因为它与登录无关!)。

有几种方法可以解决:

您可以在通常看到 .csproj 文件的项目根路径中的命令行中 运行 dotnet ef database update 之后 dotnet ef migrations add Initial

或者,您可以调用 dbContext.Database.EnsureCreated()dbContext.Database.Migrate(),但建议使用后一种 Migrate() 方法,因为它不会损坏以后的迁移。

public class Program
{
    public static void Main(string[] args)
    {
        var host = BuildWebHost(args);

        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;

            try
            {
                var dbContext = services.GetRequiredService<MyDbContext>();
                //dbContext.Database.EnsureCreated(); //ensure db is created (created database can't be later updated using migrations!)
                dbContext.Database.Migrate(); //ensure pending migrations applied and db is created 
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "An error occurred creating the DB.");
            }
        }

        host.Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}