SQL Server Express 出现 Hangfire 连接问题:用户登录失败

问题描述 投票:0回答:2

我正在尝试连接到我的

Hangfire service
中的 SQL Server Express。

我的

Startup.cs
中的代码如下所示:

services.AddHangfire(configuration => configuration
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
            {
                CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                QueuePollInterval = TimeSpan.Zero,
                UseRecommendedIsolationLevel = true,
                UsePageLocksOnDequeue = true,
                DisableGlobalLocks = true
            }));

        services.AddHangfireServer();

根据文档,Hangfire 1.7 具有

built ib sql query
来创建数据库。

我从文档中创建默认连接字符串:

"HangfireConnection": "Server=.\\sqlexpress; Database=Hangfire; Integrated Security=SSPI;"

但是当我运行我的应用程序时,我收到此错误:

System.Data.SqlClient.SqlException:'无法打开登录请求的数据库“Hangfire”。登录失败。
用户“DESKTOP-FOVJ16Q\Michal”登录失败

SQL Server Express 中的

我的

Security
选项卡如下所示:

您能告诉我,我可以检查什么才能与我的 SQL Server Express 连接吗?

编辑1

My SQL Server Express“安全”选项卡带有

Logins
:

我的

DESKTOP-FOVJ16Q\Micha
属性:

编辑2

我现在从该连接字符串检查:

"Server=.\\SQLEXPRESS;Database=TaskManager;Trusted_Connection=True;"

我可以简单地在我的

SQL Express
中创建数据库并在
Management studio

中查看它
c# sql-server ssms sql-server-express hangfire
2个回答
2
投票

如果您阅读链接到的页面上的评论(https://docs.hangfire.io/en/latest/configuration/using-sql-server.html),您会发现“Fabian Schneiter”提出的问题“谁问了与你几乎相同的问题 - 并得到了这样的答复:

您需要首先在服务器上创建数据库

Hangfire
。我明白,hangfire 也会创建数据库,但这是错误的。如果您有 Hangfire 数据库,Hangfire 将创建所有需要的表..

所以我的建议:尝试创建

Hangfire
数据库作为新数据库 - 然后再次运行您的代码,看看它是否有效


0
投票

我在第一次运行时使用静态方法生成所有需求数据库。

        public static void Generate(string databasename)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["DBCreator"].ConnectionString; ;
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        string sql = string.Format(
                @"IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'{0}')
                        BEGIN
                        create database [{0}]  
                        END;",
                databasename);



        SqlCommand command = new SqlCommand(sql, connection);
        command.ExecuteNonQuery();
        connection.Close();
    }

其连接字符串是:

        <add name="DBCreator" connectionString="Data Source=.;Initial Catalog=master;Integrated Security=SSPI;Trusted_Connection=True;" providerName="System.Data.SqlClient" />

在 Hangfire 之前的启动文件中,我首先创建数据库:

DataBaseGenerator.Generate("Hangfire");
© www.soinside.com 2019 - 2024. All rights reserved.