将数据添加到现有 DataTable 的 DataRow
我想将数据添加到数据表中已有的数据行。 我们的想法是,在不复制已存在的数据的情况下执行此操作。
在编写一些测试时,我发现直接插入数据比将现有数据和新数据复制到新行并添加该行要慢得多。
或者我做错了?
首先,我使用初始数据创建一个数据表。
填充初始数据:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道你为什么要这么努力,可能有你自己的原因。
然而,不准确的语法,你是否研究过诸如...之类的东西?
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...