Sql 服务器数据库 Asp.net

Sql Server DataBase with Asp.net

我在 asp.net

中有此代码
public static IEnumerable<Raspi_speaker> GetData()
        {

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(@"SELECT  [Raspberry_ID],[Raspberry_name],[Speaker_Name],[Speaker_Volume] FROM [ISH_EDVBetreuung].[dbo].[Raspi_speaker]", connection))
                {
                    // Make sure the command object does not already have
                    // a notification object associated with it.
                    command.Notification = null;
                    SqlDependency.Start(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString);
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == ConnectionState.Closed)
                        connection.Open();

                    using (var reader = command.ExecuteReader())
                        return reader.Cast<IDataRecord>()
                            .Select(x => new Raspi_speaker()
                            {
                              Raspberry_ID = x.GetString(0),
                              Raspberry_name = x.GetString(1),
                              Speaker_Name = x.GetString(2),
                              Speaker_Volume = x.GetInt32(4),
                                 Speaker_Mute = x.GetBoolean(5),
                              Speaker_Status = x.GetBoolean(6),
                              Speaker_Availability = x.GetBoolean(7),
                              currently_playing_song = x.GetString(8),
                              Date_Time = x.GetDateTime(9)
                            }).ToList();



                }
            }
        }
        private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            MyHub.Show();
        }

    }
}

这是 SignalR 代码,当有人在数据库中输入新内容时,此代码必须在索引页面中实时显示所有来自数据库的新刷新

我在 运行 这段代码时遇到了这个错误:

The SQL Server Service Broker for the current database is not activated. As a result, query notifications are not supported. Enable Service Broker for this database if you want to use notifications.'

我在 Sql Server Management Studio 中写了这个 Sql 命令:

ALTER database [ISH_EDVBetreuung] set enable_broker.

但我仍然遇到这个错误,我不知道为什么??

希望得到大家的帮助

非常感谢

因为它适用于 op。我将其添加为答案。 运行 下面 SSMSenable broker.

ALTER DATABASE [ISH_EDVBetreuung] SET NEW_BROKER WITH ROLLBACK IMMEDIATE;