datarow 배열을 데이터 테이블로 변환하는 간단한 방법
DataRow
배열을 DataTable
... 로 변환하고 싶습니다. 이 작업을 수행하는 가장 간단한 방법은 무엇입니까?
DataRow 배열을 반복하고 다음과 같이 추가 (필요한 경우 DataRow.ImportRow 사용)하여 다음과 같이 추가하지 않는 이유는 무엇입니까?
foreach (DataRow row in rowArray) {
dataTable.ImportRow(row);
}
dataTable에 DataRow 배열의 DataRows와 동일한 스키마가 있는지 확인하십시오.
.Net Framework 3.5 이상
DataTable dt = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt1 = dr.CopyToDataTable();
그러나 배열에 행이 없으면 The source contains no DataRows 와 같은 오류가 발생할 수 있습니다 . 따라서이 방법을 사용하기로 결정한 경우 CopyToDataTable()
배열에 데이터 행이 있는지 여부를 확인해야합니다.
if (dr.Length > 0)
DataTable dt1 = dr.CopyToDataTable();
MSDN에서 사용 가능한 참조 : DataTableExtensions.CopyToDataTable 메서드 (IEnumerable)
DataTable dt = new DataTable();
DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");
dt.Rows.Add(dr);
또 다른 방법은 DataView를 사용하는 것입니다.
// Create a DataTable
DataTable table = new DataTable()
...
// Filter and Sort expressions
string expression = "[Birth Year] >= 1983";
string sortOrder = "[Birth Year] ASC";
// Create a DataView using the table as its source and the filter and sort expressions
DataView dv = new DataView(table, expression, sortOrder, DataViewRowState.CurrentRows);
// Convert the DataView to a DataTable
DataTable new_table = dv.ToTable("NewTableName");
간단한 방법은 다음과 같습니다.
// dtData is DataTable that contain data
DataTable dt = dtData.Select("Condition=1").CopyToDataTable();
// or existing typed DataTable dt
dt.Merge(dtData.Select("Condition=1").CopyToDataTable());
DataTable dt = new DataTable();
foreach (DataRow dr in drResults)
{
dt.ImportRow(dr);
}
DataTable dt = myDataRowCollection.CopyToDataTable<DataRow>();
DataTable Assetdaterow =
(
from s in dtResourceTable.AsEnumerable()
where s.Field<DateTime>("Date") == Convert.ToDateTime(AssetDate)
select s
).CopyToDataTable();
여기에 해결책이 있습니다. 잘 작동합니다.
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);
}
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
.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()
You could use System.Linq like this:
if (dataRows != null && dataRows.Length > 0)
{
dataTable = dataRows.AsEnumerable().CopyToDataTable();
}
DataTable dataTable = new DataTable();
dataTable = OldDataTable.Tables[0].Clone();
foreach(DataRow dr in RowData.Tables[0].Rows)
{
DataRow AddNewRow = dataTable.AddNewRow();
AddNewRow.ItemArray = dr.ItemArray;
dataTable.Rows.Add(AddNewRow);
}
You need to clone the structure of Data table first then import rows using for loop
DataTable dataTable =dtExisting.Clone();
foreach (DataRow row in rowArray) {
dataTable.ImportRow(row);
}
참고URL : https://stackoverflow.com/questions/2122511/simple-way-to-convert-datarow-array-to-datatable
'code' 카테고리의 다른 글
파이썬 문자열은 불변하지 않습니까? (0) | 2020.08.29 |
---|---|
Android의 원형 그래디언트 (0) | 2020.08.29 |
Android View.getDrawingCache는 null 만 반환합니다. (0) | 2020.08.29 |
JavaHL 라이브러리를로드하지 못했습니다. (0) | 2020.08.29 |
단위 테스트에서 출력을 작성하는 방법은 무엇입니까? (0) | 2020.08.29 |