将数据添加到现有 DataTable 的 DataRow

发布于 2024-10-14 08:13:52 字数 1997 浏览 6 评论 0原文

我想将数据添加到数据表中已有的数据行。 我们的想法是,在不复制已存在的数据的情况下执行此操作。

在编写一些测试时,我发现直接插入数据比将现有数据和新数据复制到新行并添加该行要慢得多。

或者我做错了?

首先,我使用初始数据创建一个数据表。

填充初始数据:

DataTable table1 = new DataTable();
int count = 15;
for (int i = 0; i < count; i++)
{
    table1.Columns.Add("hallo" + i, i % 2 == 0 ? typeof(int) : typeof(string));
}
int newStartIndex = table1.Columns.Count;
DateTime pre = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
    DataRow row = table1.NewRow();
    for (int j = 0; j < table1.Columns.Count; j++)
    {
        if (j % 2 == 0)
        {
            row[j] = 502;
        }
        else
        {
            row[j] = "test";
        }
    }
    table1.Rows.Add(row);
}

然后我添加另外 15 列和数据。

for (int i = count; i < 2 * count; i++)
{
    table1.Columns.Add("hallo" + i, i % 2 == 0 ? typeof(int) : typeof(string));
}

foreach( DataRow row in table1.Rows)
{
    for (int j = newStartIndex; j < table1.Columns.Count; j++)
    {
        if (j % 2 == 0)
        {
            row[j] = 502;
        }
        else
        {
            row[j] = "test";
        }
    }               
}

耗时时显示,插入数据(应该和最初添加的数据一模一样)花费的时间是最初填充的10倍左右。

现在我尝试了同样的复制数据:

List<object[]> toAdd = new List<object[]>();

foreach (DataRow row in table1.Rows)
{
    object[] newArray = new object[table1.Columns.Count];
    Array.Copy(row.ItemArray, newArray, count);             
    for (int j = newStartIndex; j < table1.Columns.Count; j++)
    {
        if (j % 2 == 0)
        {
            newArray[j] = 502;
        }
        else
        {
            newArray[j] = "test";
        }
    }
    toAdd.Add(newArray);
}
table1.Rows.Clear();
foreach( var o in toAdd)
{
    table1.Rows.Add(o);
}

这大约需要初始填充的 2.5 倍时间,这使得它比直接插入快得多。

不知何故,我认为必须有一种比复制所有内容并重新添加更快的方法来添加数据。

我尝试写入 DataRow.ItemArray,但写入后更改将不会出现在 DataTable 中。

有什么想法吗?也许对这种行为的解释?

I want to add data to an already existing DataRow in a DataTable.
The idea is, to do this without copying the data that already exists.

When writing some tests, I found out that directly inserting the data is much slower than copying both the existing data and the new to a new row and add that row.

Or am I doing it wrong?

First I create a DataTable with initial data.

Fill Initial Data:

DataTable table1 = new DataTable();
int count = 15;
for (int i = 0; i < count; i++)
{
    table1.Columns.Add("hallo" + i, i % 2 == 0 ? typeof(int) : typeof(string));
}
int newStartIndex = table1.Columns.Count;
DateTime pre = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
    DataRow row = table1.NewRow();
    for (int j = 0; j < table1.Columns.Count; j++)
    {
        if (j % 2 == 0)
        {
            row[j] = 502;
        }
        else
        {
            row[j] = "test";
        }
    }
    table1.Rows.Add(row);
}

Afterwards I add another 15 columns and the data.

for (int i = count; i < 2 * count; i++)
{
    table1.Columns.Add("hallo" + i, i % 2 == 0 ? typeof(int) : typeof(string));
}

foreach( DataRow row in table1.Rows)
{
    for (int j = newStartIndex; j < table1.Columns.Count; j++)
    {
        if (j % 2 == 0)
        {
            row[j] = 502;
        }
        else
        {
            row[j] = "test";
        }
    }               
}

When taking the time, it shows that inserting the data (which should be exactly the same data as the data initially added) takes about 10 times as long as the initial filling.

Now I tried the same with copying the data:

List<object[]> toAdd = new List<object[]>();

foreach (DataRow row in table1.Rows)
{
    object[] newArray = new object[table1.Columns.Count];
    Array.Copy(row.ItemArray, newArray, count);             
    for (int j = newStartIndex; j < table1.Columns.Count; j++)
    {
        if (j % 2 == 0)
        {
            newArray[j] = 502;
        }
        else
        {
            newArray[j] = "test";
        }
    }
    toAdd.Add(newArray);
}
table1.Rows.Clear();
foreach( var o in toAdd)
{
    table1.Rows.Add(o);
}

This takes about 2.5 times as long as the initial filling, which makes it a lot faster than directly inserting.

Somehow I think there must be a faster way to add data than to copy everything and add it anew.

I tried writing to DataRow.ItemArray, but the changes will not be present in the DataTable after writing there.

Any ideas? And maybe explanations for this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-10-21 08:13:52

我不知道你为什么要这么努力,可能有你自己的原因。

然而,不准确的语法,你是否研究过诸如...之类的东西?

DataTable.Clone()  // to make a copy

DataTable.Merge()  // to merge one datatable all rows (w/Matching columns) into another

DataView oDV = YourDataTable.DefaultView
oDV.Filter = ...
DataTable newTable = oDV.ToTable()

I'm not sure why you are going through such efforts, and probably have your own reasons.
Not exact syntax, however, have you looked into things like...

DataTable.Clone()  // to make a copy

DataTable.Merge()  // to merge one datatable all rows (w/Matching columns) into another

DataView oDV = YourDataTable.DefaultView
oDV.Filter = ...
DataTable newTable = oDV.ToTable()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文