Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use SqlDependency with STRING parameter
    primarykey
    data
    text
    <p>I am trying to detect if any changes been made to SQL table I'm working with. I need to specify the search by selecting specific records only, and for this reason I need to use string parameter. I understand that according to <a href="http://msdn.microsoft.com/library/ms181122.aspx" rel="nofollow">this MSDN document</a>: </p> <p><code>The statement must not contain conditional statements that cannot change and cannot return results (for example, WHERE 1=0)</code></p> <p>Is there any way to include string parameter when using <code>SqlDependency</code>?</p> <p>If it means something, I use <code>SQL Server 2012</code> and <code>VS 2010</code></p> <p>Here is my code so far.</p> <p>Code output: <code>"The above notification query is not valid."</code>:</p> <pre><code>using System.Data; using System.Data.SqlClient; namespace AutoRegSession { public partial class RoomActiveSession : Form { public Timer timer = new Timer(); //Timer to measure update times public string SessionID; //String to hold selected sessionID string ConnStr = "Data Source=DUZY;Initial Catalog=AutoRegSQL;Integrated Security=True"; SqlDependency dependency; public RoomActiveSession() { SqlDependency.Start(ConnStr); InitializeComponent(); } private void btn_Exit_Click(object sender, EventArgs e) { SqlDependency.Stop(ConnStr); timer.Enabled = false; //Disable timer timer.Stop(); //Stop timer Application.Exit(); //Close application } //Check for table updates every 3 seconds private void timer_Tick(object sender, EventArgs e) { refreshDGV(); } //SQL query that returns current/updated attendance result list for the given SessionID public void refreshDGV() { DataTable queryResult = new DataTable(); SqlConnection MyConn = new SqlConnection(ConnStr); //Use connection string string query = @"SELECT TagID, SessionID, ScanningTime" + " FROM Attendance " + " WHERE SessionID = @SessionID "; SqlCommand command = new SqlCommand(query, MyConn); command.Parameters.Add("SessionID", SqlDbType.Char).Value = SessionID; //Create a dependency and associate it with the SqlCommand SqlDependency dependency = new SqlDependency(command); //Subscribe to the SqlDependency event dependency.OnChange += new OnChangeEventHandler(OnDependencyChange); SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(queryResult); DGVSetDataSouce(queryResult); } //Handler method for SQL Dependecy private void OnDependencyChange(object sender, SqlNotificationEventArgs eventArgs) { if (eventArgs.Info == SqlNotificationInfo.Invalid) { MessageBox.Show("The above notification query is not valid."); } else { MessageBox.Show("Notification Info: " + eventArgs.Info); MessageBox.Show("Notification source: " + eventArgs.Source); MessageBox.Show("Notification type: " + eventArgs.Type); } } //Create and start the timer public void SetTimer() { timer.Interval = 3000; timer.Tick += new EventHandler(timer_Tick); timer.Enabled = true; timer.Start(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload