S
Stefan
Hey all,
I tried to use SqlDependency in WinFormsApplication to show up with new
information upon a change in a SqlResult.
The Problem I have is that the OnChangeEvent is fired when I create the
SqlDependency, but not again afterwards.
Any idea on that?
Source:
namespace sql_dependency
{
public partial class Form1 : Form
{
public System.Data.SqlClient.SqlConnection SqlConn;
public Form1()
{
InitializeComponent();
System.Data.SqlClient.SqlDependency.Start([serverconnectionstring]);
SqlConn = new
System.Data.SqlClient.SqlConnection([serverconnectionstring]);
}
private void SqlSelect_TextChanged(object sender, EventArgs e)
{
if(SqlSelect.Text == String.Empty)
DepCheckActivateBtn.Enabled = false;
else
DepCheckActivateBtn.Enabled = true;
}
private void DepCheckActivateBtn_Click(object sender, EventArgs e)
{
// Assume c is an open SqlConnection.
SqlConn.Open();
// Create a new SqlCommand object.
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand("SELECT * FROM MyTestTable", SqlConn);
// Create a dependency and associate it with the SqlCommand..
System.Data.SqlClient.SqlDependency dep = new
System.Data.SqlClient.SqlDependency(cmd);
// Maintain the refence in a class member.
// Subscribe to the SqlDependency event.
dep.OnChange += new
System.Data.SqlClient.OnChangeEventHandler(SqlResultChanged);
// Execute the command.
cmd.ExecuteReader();
// Process the DataReader.
}
private void SqlResultChanged(object sender, EventArgs e)
{
MessageBox.Show("Result changed!");
}
}
}
I tried to use SqlDependency in WinFormsApplication to show up with new
information upon a change in a SqlResult.
The Problem I have is that the OnChangeEvent is fired when I create the
SqlDependency, but not again afterwards.
Any idea on that?
Source:
namespace sql_dependency
{
public partial class Form1 : Form
{
public System.Data.SqlClient.SqlConnection SqlConn;
public Form1()
{
InitializeComponent();
System.Data.SqlClient.SqlDependency.Start([serverconnectionstring]);
SqlConn = new
System.Data.SqlClient.SqlConnection([serverconnectionstring]);
}
private void SqlSelect_TextChanged(object sender, EventArgs e)
{
if(SqlSelect.Text == String.Empty)
DepCheckActivateBtn.Enabled = false;
else
DepCheckActivateBtn.Enabled = true;
}
private void DepCheckActivateBtn_Click(object sender, EventArgs e)
{
// Assume c is an open SqlConnection.
SqlConn.Open();
// Create a new SqlCommand object.
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand("SELECT * FROM MyTestTable", SqlConn);
// Create a dependency and associate it with the SqlCommand..
System.Data.SqlClient.SqlDependency dep = new
System.Data.SqlClient.SqlDependency(cmd);
// Maintain the refence in a class member.
// Subscribe to the SqlDependency event.
dep.OnChange += new
System.Data.SqlClient.OnChangeEventHandler(SqlResultChanged);
// Execute the command.
cmd.ExecuteReader();
// Process the DataReader.
}
private void SqlResultChanged(object sender, EventArgs e)
{
MessageBox.Show("Result changed!");
}
}
}