SQL Dependency C#
Как заставить работать? Взял пример с msdn, вставил в свою формочку:
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SqlDependency.Start(conString);
}
string conString = @"SERVER=alex\srv; DATABASE=myBase; Trusted_Connection=True;";
SqlConnection connection;
void SomeMethod()
{
// Assume connection is an open SqlConnection.
connection = new SqlConnection(conString);
connection.Open();
// Create a new SqlCommand object.
using (SqlCommand command = new SqlCommand(
"SELECT Id, Number FROM dbo.ProductionTask",
connection))
{
// Create a dependency and associate it with the SqlCommand.
SqlDependency dependency = new SqlDependency(command);
// Maintain the reference in a class member.
// Subscribe to the SqlDependency event.
dependency.OnChange += new
OnChangeEventHandler(OnDependencyChange);
// Execute the command.
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
tb1.Text += $"\r\n {reader.GetInt32(0)} - {reader.GetString(1)}";
}
}
}
}
}
// Handler method
void OnDependencyChange(object sender,
SqlNotificationEventArgs e)
{
tb1.Text += $"\r\n {e.Info.ToString()}";
}
void Termination()
{
connection.Close();
SqlDependency.Stop(conString);
}
private void sqlStart_Click(object sender, EventArgs e)
{
SomeMethod();
}
private void sqlStop_Click(object sender, EventArgs e)
{
Termination();
}
}
}
не работает. Я меняю поле в базе данных, но уведомление не приходит. Брокер включен в базе данных. Исключений нет Куда копать?
Ответы (1 шт):
Автор решения: Kuan-kek
→ Ссылка
Проблема решилась, когда я поменял в SQL Server database owner на SA. Не понимаю, почему это имеет значение?