c# - 如何使用带有 STRING 参数的 SqlDependency

标签 c# sql winforms parameters sqldependency

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

语句中不能包含不能改变也不能返回结果的条件语句(例如,WHERE 1=0)

在使用SqlDependency时有没有办法包含字符串参数?

如果这意味着什么,我使用 SQL Server 2012VS 2010

到目前为止,这是我的代码。

代码输出:“上述通知查询无效。”:

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子句不起作用。

关于c# - 如何使用带有 STRING 参数的 SqlDependency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301116/

相关文章:

c# - 使用 AutoMapper 将字典的值映射到列表

javascript - 标签未添加到 Google map 标记

php - 使用 MySQL 查询在列名中查找字符串

c# - 如何检查窗体是否打开了任何 ShowDialog() 窗体?

c# - 从 PictureBox 中获取指定颜色的所有像素点

c# - 让 HttpClient 使用 app.config defaultProxy

c# - 如何向连续的 Azure WebJob 通知队列上的新消息?

sql - 使用 SQL 导出获取图像 - Prestashop

mysql - MySQL中的索引查询

c# - 如何使 Windows 窗体中的多个控件随窗口自动调整大小?