I'd like to use SqlDependency
class in my winforms application :
public partial class Form1 : Form
{
private int changeCount = 0;
private const string statusMessage = "{0} changes have occurred.";
private static SqlConnection connection = null;
private static SqlCommand command = null;
private static SqlDependency dependency;
private static SqlCommand command1 = null;
private static SqlDependency dependency1;
public Form1()
{
InitializeComponent();
button1.Enabled = CanRequestNotifications();
this.FormClosed += Form1_FormClosed;
if (connection == null)
{
connection = new SqlConnection(GetConnectionString());
}
if (command == null)
{
command = new SqlCommand("procCreationUser", connection);
command.CommandType = CommandType.StoredProcedure;
}
dependency = new SqlDependency(command);
if (connection == null)
{
connection = new SqlConnection(GetConnectionString());
}
if (command1 == null)
{
command1 = new SqlCommand("procSelectionUser", connection);
command1.CommandType = CommandType.StoredProcedure;
}
dependency1 = new SqlDependency(command1);
GetData();
}
private void GetData()
{
SqlDependency.Start(GetConnectionString());
if (connection.State != ConnectionState.Open) connection.Open();
using (var dr = command.ExecuteReader())
{
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
}
using (var dr = command1.ExecuteReader())
{
dependency1.OnChange += new OnChangeEventHandler(dependency_OnChange);
}
}
private string GetConnectionString()
{
return @"Data Source=PRT-12\SQLEXPRESS; Initial Catalog=TestNotification;Integrated Security=True";
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
MessageBox.Show("Notification");
}
}
I created two stored procedures : procCreationUser
for creation and procSelectionUser
to select users.
When I launched the application, and I clicked into the button and insert a new row in the database I haven't no notification !!!!!
I recommend you read these:
You need to establish that the following event all occur:
sys.dm_qn_subscriptions
transmission_status
in sys.transmission_queue
SqlDependency.Start
once per appdomain and ideally should call SqlDependency.Stop
when the appdomain shuts down.Once you get the notification flowing make sure you check and honor the Source
, Info
and Type
in your SqlNotificationEventArgs
arg. Not all combinations indicate success, the notification may also fire immediately indicating a problem with your subscription.
Also, a small tidbits with your code:
you need to hook up the SqlDependency before executing the query:
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); using (var dr = command.ExecuteReader()) { while (dr.Read()) { ... } }
you need to call SqlDependency.Start
once per appdomain. Try calling it in Main, before displaying the Form