首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MS SQL表中的新行之后执行C#代码

如何在MS SQL表中的新行之后执行C#代码
EN

Stack Overflow用户
提问于 2016-04-04 21:32:23
回答 1查看 78关注 0票数 0

我想标题就是这么说的。

在MS SQL-Table获得新行后,我尝试执行C#代码。有人能给我解释一下怎么做吗?

我用C#-Class 'SqlDependency‘尝试过,但我认为,这不是正确的解决方案,因为C#-代码必须总是重新启动。

我应该用T-SQL和SQL-Agent或类似的东西来尝试它吗?也许我在C #上完全错了,我不知道,我真的需要你的帮助!

我是新来的,所以请对我耐心一点:)

来自奥地利加斯扎的问候

EN

回答 1

Stack Overflow用户

发布于 2016-04-04 21:39:40

如果你想要一个简单的解决方案,你可以试试这个:

使用System.Data.SqlClient连接到您的Database

然后,您可以使用计时器在特定的时间间隔运行"RowCountCheck“:

代码语言:javascript
复制
Timer timer = new Timer();      //using using System.Timers;
timer.Interval = 5000;          //Time for 1 interval in milliseconds
timer.AutoReset = true;         //Start again when elapsed
timer.Elapsed += Timer_Elapsed; //Method called when timer elapsed

private int _lastRowCount;

private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
   //Check row Count
   //Use your SqlClient here to get the rowCount
   

   if(_lastRowCount < currentRowCount)
   {
      //Row was added: Do your work here:
   }


  //Set the currentRowCount as _lastRowCount for the next check
   _lastRowCount = "yourValue";
}

链接:

http://www.w3schools.com/sql/sql_func_count.asp -如何从SQL表中获取RowCount

https://msdn.microsoft.com/de-de/library/system.data.sqlclient(v=vs.110).aspx - SqlClient

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36404161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档