如何最小化SqlTableDependency中的调用

时间:2018-08-07 07:23:01

标签: c# signalr

如果在表中插入100行,则仅应发送一次调用以获取值。

public static void InitiateSQLTableDependency()
    {
        try
        {
            var mapper = new ModelToTableMapper<DeviceValuesProperties>();
            mapper.AddMapping(deviceValue => deviceValue.DeviceId, "DeviceId");
            mapper.AddMapping(deviceValue => deviceValue.TimeStamp, "TimeStamp");

            tableDependency = new SqlTableDependency<DeviceValuesProperties>(connectionString, "DeviceValues", mapper);
            tableDependency.OnChanged += OnDependencyChange;
            tableDependency.OnError += OnDependencyError;
            tableDependency.Start();
            isSQLDependencyStart = true;
        }
        catch (Exception exception)
        {
            throw exception;
        }
        }

1 个答案:

答案 0 :(得分:0)

不幸的是,它不是那样工作的(假设您正在使用此lib [{https://github.com/christiandelbianco/monitor-table-change-with-sqltabledependency]或什至是标准SqlDependency。您希望多久触发一次?每100行?还是每x个时间单位?为什么?不只是每隔一段时间轮询一次表吗?如果您想要自动进​​行批量访问-您应该研究CDC,CDT还是自己编写触发器;

https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/about-change-data-capture-sql-server?view=sql-server-2017

https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/work-with-change-tracking-sql-server?view=sql-server-2017