Home > Blockchain >  Sql Server DataBase with Asp.net
Sql Server DataBase with Asp.net

Time:05-11

I have this code in 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();
        }

    }
}

this is SignalR code this code must bring all New Refresh from DataBase in live Display in in index page when someone enter new thing in database

my porplem when Run this code I got this error:

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

I wrote this Sql command in Sql Server Management Studio:

ALTER database [ISH_EDVBetreuung] set enable_broker.

but I still got this error I dont know why??

i hope your help please

thanks so much

CodePudding user response:

As it is working for op. I am adding this as an answer. Run below in SSMS to enable broker.

ALTER DATABASE [ISH_EDVBetreuung] SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
  • Related