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

时间:2016-05-11 12:43:59

标签: c# sql-server sql-server-express localdb

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

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

它会引发下一个错误:

  

用户'

登录失败

如果我删除 .mdf 扩展程序,则会抛出相同的错误

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

Integrated Security = true

它抛出了这个:

  

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

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

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

请帮忙吗?

6 个答案:

答案 0 :(得分:8)

注意:如果您将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.

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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

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

您未使用用户名或密码的陈述没有任何意义。您必须在数据库上拥有用户名和密码。

答案 3 :(得分:1)

这个没有回答所以我认为我列出了我的建议,似乎也出现在VS社区2015中,以管理员身份运行VS,似乎解决了这个问题。

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

也许其他人可以回答为什么这是必需的,只要知道管理员需要很多VS功能才能正常工作,不知道为什么它首先没有启用。

非常讨厌只使用本地开发数据库..应该只是工作没有所有这些摆弄和错误信息。

答案 4 :(得分:1)

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

答案 5 :(得分:1)

在ASP.Net Core中,如果尚未创建数据库,可能会引发类似错误! (错误消息可能与您的登录无关,可能会使您感到困惑!)。

有几种解决方法:

您可以在命令行中通常会看到.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();
}
相关问题