Simple way to convert datarow array to datatable

2019-01-21 05:12发布

I want to convert a DataRow array into DataTable ... What is the simplest way to do this?

13条回答
▲ chillily
2楼-- · 2019-01-21 05:39

Here is the solution. It should work fine.

DataTable dt = new DataTable();
dt = dsData.Tables[0].Clone();
DataRows[] drResults = dsData.Tables[0].Select("ColName = 'criteria');

foreach(DataRow dr in drResults)
{
    object[] row = dr.ItemArray;
    dt.Rows.Add(row);
} 
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-21 05:43

Incase anyone needs it in VB.NET:

Dim dataRow as DataRow
Dim yourNewDataTable as new datatable
For Each dataRow In yourArray
     yourNewDataTable.ImportRow(dataRow)
Next
查看更多
倾城 Initia
4楼-- · 2019-01-21 05:44
DataTable dt = new DataTable();
foreach (DataRow dr in drResults)
{ 
    dt.ImportRow(dr);
}   
查看更多
对你真心纯属浪费
5楼-- · 2019-01-21 05:45

You could use System.Linq like this:

if (dataRows != null && dataRows.Length > 0)
{
   dataTable = dataRows.AsEnumerable().CopyToDataTable();
}
查看更多
姐就是有狂的资本
6楼-- · 2019-01-21 05:47

.Net 3.5+ added DataTableExtensions, use DataTableExtensions.CopyToDataTable Method

For datarow array just use .CopyToDataTable() and it will return datatable.

For single datarow use

new DataRow[] { myDataRow }.CopyToDataTable()
查看更多
甜甜的少女心
7楼-- · 2019-01-21 05:49
DataTable dt = new DataTable(); 

DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");

dt.Rows.Add(dr);
查看更多
登录 后发表回答