Всем привет я создаю проект на asp.net где использую SqlDependency для отслеживание изменений в базе но оно не работает. вот код
public List<Chat> getChat()
{
string Operator_ID = "1";
SqlDataReader reader;
List<Chat> ListChat = new List<Chat>();
using (SqlConnection sql_connection=new SqlConnection(connString))
{
SqlCommand sql_command = new SqlCommand("Select ID as Chat_ID, Subject_ID as Subject, User_ID as Phone_Number, Private_Auth, Client_Name, Client_ID,(Select COUNT(*) from MIB_Message_Comment where Status_Read=0 AND Sender_User_ID=User_ID AND Chat_ID=ID)as Count, Status from MIB_Message_Chat where Online_Consulting=1 And Operator_ID=1", sql_connection);
sql_command.Parameters.Add(new SqlParameter("Operator_Id", Operator_ID));
sql_command.Notification = null;
SqlDependency dependency = new SqlDependency(sql_command);
dependency.OnChange += new OnChangeEventHandler(OnChange_Chat);
if (sql_connection.State==ConnectionState.Closed)
{
sql_connection.Open();
}
reader = sql_command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Chat chat = new Chat();
chat.Phone_Number = reader["Phone_Number"].ToString();
chat.Num_Chat = reader["Chat_ID"].ToString();
chat.Private_Auth = reader["Private_Auth"].ToString();
chat.Status = reader["Status"].ToString();
chat.Subject = reader["Subject"].ToString();
ListChat.Add(chat);
}
}
return ListChat;
}
}
void OnChange_Chat(Object sender, SqlNotificationEventArgs e)
{
index chat = new index();
chat.UpdateListCHat();
}
А это уже метод для обработки измениний
public void UpdateListCHat()
{
var lisChat = chatData.getChat();
foreach (var item in lisChat)
{
ListBox1.Items.Add(item.Num_Chat + " " + item.Phone_Number + " " + item.Subject);
}
}
Я для строки соединение поставил старт