C# 将多行添加到数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18462915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding multiple rows to DataTable
提问by Anuj
I know two ways to add new row with data to a DataTable
我知道两种将带有数据的新行添加到 DataTable
string[] arr2 = { "one", "two", "three" };
dtDeptDtl.Columns.Add("Dept_Cd");
for (int a = 0; a < arr2.Length; a++)
{
DataRow dr2 = dtDeptDtl.NewRow();
dr2["Dept_Cd"] = DeptCd[a];
dtDeptDtl.Rows.Add(dr2);
}
for (int a = 0; a < arr2.Length; a++)
{
dtDeptDtl.Rows.Add();
dtDeptDtl.Rows[a]["Dept_Cd"] = DeptCd[a];
}
Both the above methods will give me the same result i.e One Two Three will be added in DataTable
in seperate rows.
上述两种方法都会给我相同的结果,即一二三将被添加DataTable
到单独的行中。
But my question is that what is the difference between both the steps and which one is better way performance wise?
但我的问题是,这两个步骤之间有什么区别,哪一个更好地提高性能?
采纳答案by Alex Filipovici
Some decompiler observations
一些反编译器观察
In both scenarios, a different overload of the System.Data.DataRowCollection.Add
method is being used.
在这两种情况下,都使用了不同的System.Data.DataRowCollection.Add
方法重载。
The first approach uses:
第一种方法使用:
public void Add(DataRow row)
{
this.table.AddRow(row, -1);
}
The second approach will use:
第二种方法将使用:
public DataRow Add(params object[] values)
{
int record = this.table.NewRecordFromArray(values);
DataRow dataRow = this.table.NewRow(record);
this.table.AddRow(dataRow, -1);
return dataRow;
}
Now, take a look at this little beast:
现在,看看这个小野兽:
internal int NewRecordFromArray(object[] value)
{
int count = this.columnCollection.Count;
if (count < value.Length)
{
throw ExceptionBuilder.ValueArrayLength();
}
int num = this.recordManager.NewRecordBase();
int result;
try
{
for (int i = 0; i < value.Length; i++)
{
if (value[i] != null)
{
this.columnCollection[i][num] = value[i];
}
else
{
this.columnCollection[i].Init(num);
}
}
for (int j = value.Length; j < count; j++)
{
this.columnCollection[j].Init(num);
}
result = num;
}
catch (Exception e)
{
if (ADP.IsCatchableOrSecurityExceptionType(e))
{
this.FreeRecord(ref num);
}
throw;
}
return result;
}
Especially, note the this.columnCollection[i][num] = value[i];
, which will call:
尤其要注意this.columnCollection[i][num] = value[i];
, 它将调用:
public DataColumn this[int index]
{
get
{
DataColumn result;
try
{
result = (DataColumn)this._list[index];
}
catch (ArgumentOutOfRangeException)
{
throw ExceptionBuilder.ColumnOutOfRange(index);
}
return result;
}
}
Moving forward, we discover that actually _list
is an ArrayList
:
继续前进,我们发现实际上_list
是一个ArrayList
:
private readonly ArrayList _list = new ArrayList();
Conclusion
结论
In order to summarize the above, if you are using dtDeptDtl.Rows.Add();
instead of dtDeptDtl.Rows.Add(dr2);
, you will get a performance degradationwhich will increase exponentially, as the number of columns grows. The responsible line for the degradation is call to the NewRecordFromArray
method, which iterates over an ArrayList
.
为了总结以上内容,如果您使用的是dtDeptDtl.Rows.Add();
而不是dtDeptDtl.Rows.Add(dr2);
,则会导致性能下降,随着列数的增加,该性能将呈指数增长。负责降级的行是对NewRecordFromArray
方法的调用,该方法迭代ArrayList
.
Note: This can be easily tested if you add, let's say, 8 columns to the table and make some tests in a for
looping 1000000 times.
注意:如果您向表中添加 8 列并在for
循环 1000000 次中进行一些测试,则可以轻松测试。