【问题标题】:Cc# SQL query update when table changes?表更改时 Cc# SQL 查询更新?
【发布时间】:2014-09-23 14:33:49
【问题描述】:

有什么方法可以让 ms sql 表在每次表更改时发送信息? 我希望每次有人向表中插入值时更改 ToolStripMenuItem 的标题。

我做了一个计时器,每x秒更新一次,但如果它只在sql表发生变化时更新会更好。

我的代码:

//Rexton ordre klar til bestilling
command.CommandText = "SELECT COUNT(*) from bestillinger WHERE firma = @rexton and udlevering BETWEEN @date and @dateadd";
command.Parameters.AddWithValue("@bernafon", "Bernafon");
command.Parameters.AddWithValue("@gn_resound", "GN Resound");
command.Parameters.AddWithValue("@oticon", "Oticon");
command.Parameters.AddWithValue("@phonak", "Phonak");
command.Parameters.AddWithValue("@rexton", "Rexton");
command.Parameters.AddWithValue("@siemens", "Siemens");
command.Parameters.AddWithValue("@widex", "Widex");

con.Open();
command.ExecuteNonQuery();
string result = command.ExecuteScalar().ToString();

con.Close();

if (result != "0")
{
    rextonToolStripMenuItem.Text = "rexton " + result;
    rextonToolStripMenuItem.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF1919");
}

【问题讨论】:

标签: c# sql winforms


【解决方案1】:

您可以使用SqlDependency 类并订阅您的处理程序方法以获取更改事件

这里是 msdn 链接 msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx

SqlDependency sqlDep=new SqlDependency(SqlCommand);

sqlDep.OnChange+=new
   OnChangeEventHandler(OnChangeEventHandler);

【讨论】:

猜你喜欢
  • 2020-08-04
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
相关资源
最近更新 更多