简体   繁体   English

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

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

And it throws the next error: 它会抛出下一个错误:

Login failed for user ' 用户登录失败'

If i remove the .mdf extension it throws the same error 如果我删除.mdf扩展名,它会抛出相同的错误

Now if i'll add the following to the string: 现在,如果我将以下内容添加到字符串:

Integrated Security = true

It throws this: 它抛出这个:

Cannot open database "Database1.mdf" requested by the login. 无法打开登录请求的数据库“Database1.mdf”。 The login failed. 登录失败。 Login failed for user 'rBcollo-PC\\rBcollo'. 用户'rBcollo-PC \\ rBcollo'登录失败。

PS for Integrated Security = false it throws the "Login failed for user'" PS for Integrated Security = false它会抛出“登录失败的用户”

The problem is that i'm not using any username or password for the Database. 问题是我没有使用数据库的任何用户名或密码。

Any help,please? 有什么帮助吗?

Note: My answer would be applicable if you are using EntityFramework Core with SQL Server 注意:如果您将EntityFramework CoreSQL Server一起使用,我的答案将适用

This error could be because of the reason ' Database does not exist ' and application try to perform DB operations. 此错误可能是因为“ Database does not exist ”并且应用程序尝试执行数据库操作。

All you need to use the following line before performing any database operation. 在执行任何数据库操作之前,您只需使用以下行。

_context.Database.EnsureCreated(); 

And what is 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.

as soon as you use integrated security = true, it uses your windows credentials to login to the sql instance. 只要使用集成的security = true,它就会使用您的Windows凭据登录到sql实例。 you have to give this user the permissions to the sql instance. 您必须为此用户授予sql实例的权限。 if you don't specify integrated security = true, then its logging in as an anonymous user and that's why Login failed for user'.'. 如果你没有指定integrated security = true,那么它以匿名用户身份登录,这就是用户'登录失败的原因'。'。 you could use the third option of creating a sql account and then passing them in the connection string and login. 你可以使用第三个选项来创建一个sql帐户,然后在连接字符串和登录中传递它们。 depends on what you want to do. 取决于你想做什么。

If you set Integrated Security = true then the system will attempt to login to the database with your current windows credentials. 如果设置Integrated Security = true,则系统将尝试使用您当前的Windows凭据登录数据库。 If you have not specifically given permission to the current user in your database then this wont work. 如果您没有特别授予数据库中当前用户的权限,那么这将不起作用。

If you set Integrated Security = false or do not have the Integrated Security option at all then you need to supply the username and password. 如果您设置Integrated Security = false或根本没有Integrated Security选项,则需要提供用户名和密码。

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

Your statement that you're not using a username or password doesnt make any sense. 您没有使用用户名或密码的声明没有任何意义。 You must have a username and password on a database. 您必须在数据库上拥有用户名和密码。

This wasn't answered so I thought I'd list my suggestion, seems to be present in VS Community 2015 also, run VS as an administrator and it seems to fix the problem. 这没有回答所以我认为我列出了我的建议,似乎也出现在VS社区2015中,以管理员身份运行VS,似乎解决了这个问题。

Even though the database was created in visual studio running under my account, and the web services are running under my account, can't seem to access the Database without admin. 即使数据库是在我的帐户下运行的visual studio中创建的,并且Web服务在我的帐户下运行,但似乎无法在没有管理员的情况下访问数据库。

Maybe someone else can answer why this is required, just know admin is required for a lot of VS functionality to work correctly, not sure why its not enabled in the first place. 也许其他人可以回答为什么这是必需的,只要知道管理员需要很多VS功能才能正常工作,不知道为什么它首先没有启用。

Very annoying just to use a local dev database.. should just work without all this fiddling and misinformation. 非常烦人的只是使用本地开发数据库..应该只是工作没有所有这些摆弄和错误的信息。

If you're using netcore, don't forget to run dotnet ef database update in the project directory. 如果您使用的是netcore,请不要忘记在项目目录中运行dotnet ef database update I was getting the same error and that fixed it for me. 我得到了同样的错误,并为我修复了它。

In ASP.Net Core, similar error may be raised if the database is not created yet! 在ASP.Net Core中,如果尚未创建数据库,则可能会引发类似的错误! (the error message may confuse you as it does not related to login!). (错误消息可能会使您感到困惑,因为它与登录无关!)。

There are several ways to solve it: 有几种方法可以解决它:

You can run dotnet ef database update after dotnet ef migrations add Initial in the command line in root path of project where you usually see .csproj file. 您可以在dotnet ef migrations add Initial后运行dotnet ef database update在项目的根路径中的命令行中dotnet ef migrations add Initial ,您通常会看到.csproj文件。

Alternatively, you may call dbContext.Database.EnsureCreated() or dbContext.Database.Migrate() but the latter Migrate() method is recommended as it doesn't damage later migrations. 或者,您可以调用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法打开登录请求的数据库“ [数据库]”。 登录失败。 用户“ [User]”的登录失败 - Cannot open database “[Database]” requested by the login. The login failed. Login failed for user '[User]" 无法打开登录请求的数据库“身份”。 登录失败。 用户“DESKTOP\User”登录失败 - Cannot open database “Identity” requested by the login. The login failed. Login failed for user 'DESKTOP\User' SqlException:无法打开登录请求的数据库“ path \\ db.mdf”。 登录失败。 用户“ NT AUTHORITY \\ SYSTEM”的登录失败 - SqlException: Cannot open database “path\db.mdf” requested by the login. The login failed. Login failed for user 'NT AUTHORITY\SYSTEM' 无法打开登录请求的数据库“MyBase”。 登录失败。 用户“域\用户”登录失败 - Cannot open database "MyBase" requested by the login. The login failed. Login failed for user 'domain\User' 无法打开登录请求的数据库“ MyDb”。 登录失败。 用户“ My-PC \\\\ MrD”的登录失败 - Cannot open database “MyDb” requested by the login. The login failed. Login failed for user 'My-PC\\MrD' 无法打开登录请求的数据库“ Inventory_New”。 登录失败。 用户“ sa”的登录失败 - Cannot open database “Inventory_New” requested by the login. The login failed. Login failed for user 'sa' 无法打开登录请求的数据库“ ASPState”。 登录失败。 用户“ xxxx”的登录失败 - Cannot open database “ASPState” requested by the login. The login failed. Login failed for user 'xxxx' 无法打开登录请求的数据库“test”。 登录失败。 用户 &#39;Domain\\userid&#39; 登录失败 - Cannot open database "test" requested by the login. The login failed. Login failed for user 'Domain\userid' 无法打开登录请求的数据库“ FingerPR”。 登录失败。 用户“ NR-PC \\ NR”的登录失败 - Cannot open database “FingerPR” requested by the login. The login failed. Login failed for user 'NR-PC\NR' 无法打开登录请求的数据库“Test”。 登录失败。 用户&#39;sa&#39;登录失败 - Cannot open database “Test” requested by the login. The login failed. Login failed for user 'sa'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM