是否有任何方法来加速datatable.LoadDataRow()

本文关键字:datatable LoadDataRow 加速 任何 方法 是否 | 更新日期: 2023-09-27 18:12:25

我通过调用DataTable.LoadDatatRow来刷新一个数据表,如下所示:

public void FillTable(DataTable myTable, IEnumerable<MyObject> readings)
{
    var index=0;
    foreach(var reading in readings) {
        LoadRow(myTable, reading, index++); 
    }
}
protected override void LoadRow(DataTable table, MyObject objectValue, int index)
{
    table.LoadDataRow(
        new object[] { 
            objectValue.property1 + "a", 
            objectValue.property2 + "b", 
            /* ... etc. */
        }, LoadOption.OverwriteChanges);
}

现在,这是为了可读性而简化的,LoadDataRow行有点长(大约9个数组值),但不包含大的函数调用或任何东西,只是格式化字符串和一些?案例。当我的表中有大约50个值时,这段代码工作得很好,并且我得到了大约10 Hz的刷新。但一旦我获得比正常值更多的值(比如200),它就会呈指数增长,得到~0.2 Hz。这太慢了。

有人知道为什么当我有一个大的读数时,我得到指数级的减速吗?这是否与OverWriteChanges有关(我很确定我需要其他原因);或者可能在LoadDataRow中创建对象,或者有其他原因?

我玩过不同的LoadOptions,这似乎不是问题。我只是无法解释为什么我看到处理时间增加了这么多,我也不知道如何解决这个问题。

是否有任何方法来加速datatable.LoadDataRow()

在调用LoadRow之前和之后添加对BeginLoadData()和EndLoadData()的调用

BeginLoadData关闭事件通知、索引维护和与新数据处理相关的约束检查。这可以提高LoadDataRow的性能。

public void FillTable(DataTable myTable, IEnumerable<MyObject> readings)
{
    var index=0;
    try
    {
        dt.BeginLoadData();
        foreach(var reading in readings) {
            LoadRow(myTable, reading, index++); 
        }
    }
    finally
    {
        dt.EndLoadData();
    }
}

在出现故障的情况下,一定要恢复正常的处理。