如何将SqlDependency与STRING参数一起使用



我正在尝试检测是否对正在使用的SQL表进行了任何更改。我需要通过只选择特定记录来指定搜索,因此我需要使用字符串参数。据我所知,根据MSDN文档:

The statement must not contain conditional statements that cannot change and cannot return results (for example, WHERE 1=0)

在使用SqlDependency时,是否有任何方法可以包含字符串参数?

如果它有意义,我使用SQL Server 2012VS 2010

这是我迄今为止的代码。

代码输出:"The above notification query is not valid.":

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();
        }
    }
}

它应该可以工作。

问题可能是您没有为表n SQL查询指定两个部分的名称。

SELECT语句中的投影列必须显式声明,并且表名必须使用由两部分组成的名称限定

它只是说,当where子句在任何情况下都不能返回结果时,它就不起作用了。

相关内容

  • 没有找到相关文章

最新更新