DataTable의 열만 다른 DataTable에 복사하는 방법은 무엇입니까?
DataTable의 열만 다른 DataTable에 복사하는 방법은 무엇입니까?
DataTable.Clone()
트릭을해야합니다.
DataTable newTable = originalTable.Clone();
열만 필요한 경우 DataTable.Clone()
사용할 수 있습니다. 로 Clone
기능만을 스키마 복사됩니다. 그러나 DataTable.Copy()
구조와 데이터를 모두 복사합니다.
예
DataTable dt = new DataTable();
dt.Columns.Add("Column Name");
dt.Rows.Add("Column Data");
DataTable dt1 = dt.Clone();
DataTable dt2 = dt.Copy();
dt1
열이 하나만 있지만 dt2
행이 하나 인 열이 하나 있습니다.
Datatable.Clone
큰 테이블의 경우 느립니다. 나는 현재 이것을 사용하고 있습니다 :
Dim target As DataTable =
New DataView(source, "1=2", Nothing, DataViewRowState.CurrentRows)
.ToTable()
이것은 데이터가 아닌 소스 테이블의 구조 만 복사합니다.
이 DataTable.Clone()
방법은 완전히 새로운 DataTable을 만들려는 경우 훌륭하게 작동하지만 한 DataTable의 스키마 열을 다른 기존 DataTable 에 추가하려는 경우가있을 수 있습니다 .
예를 들어 DataTable에서 새 하위 클래스를 파생하고 여기에 스키마 정보를 가져 오려는 경우 Clone ()을 사용할 수 없습니다.
예 :
public class CoolNewTable : DataTable {
public void FillFromReader(DbDataReader reader) {
// We want to get the schema information (i.e. columns) from the
// DbDataReader and
// import it into *this* DataTable, NOT a new one.
DataTable schema = reader.GetSchemaTable();
//GetSchemaTable() returns a DataTable with the columns we want.
ImportSchema(this, schema); // <--- how do we do this?
}
}
대답은 스키마 테이블의 열을 템플릿으로 사용하여 기존 DataTable에 새 DataColumns를 만드는 것입니다.
즉, ImportSchema의 코드는 다음과 같습니다.
void ImportSchema(DataTable dest, DataTable source) {
foreach(var c in source.Columns)
dest.Columns.Add(c);
}
또는 Linq를 사용하는 경우 :
void ImportSchema(DataTable dest, DataTable source) {
var cols = source.Columns.Cast<DataColumn>().ToArray();
dest.Columns.AddRange(cols);
}
이것은 완전히 새로운 DataTable을 만들기 위해 Clone ()을 사용하지 않고 하나의 DataTable에서 다른 DataTable로 스키마 / 열을 복사하려는 상황의 한 예일뿐입니다. 나는 다른 몇 가지도 만났을 것이라고 확신합니다.
열 머리글 (데이터 없음)이있는 특정 데이터 테이블 (dataTable1)의 구조를 다른 데이터 테이블 (dataTable2)로 만들려면 다음 코드를 따를 수 있습니다.
DataTable dataTable2 = dataTable1.Clone();
dataTable2.Clear();
이제 조건에 따라 dataTable2를 채울 수 있습니다. :)
If you want to copy the DataTable
to another DataTable
of different Schema Structure
then you can do this:
- Firstly Clone the first
DataType
so that you can only get the structure. - Then alter the newly created structure as per your need and then copy the data to newly created
DataTable
.
So:
Dim dt1 As New DataTable
dt1 = dtExcelData.Clone()
dt1.Columns(17).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(26).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(30).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(35).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(38).DataType = System.Type.GetType("System.Decimal")
dt1 = dtprevious.Copy()
Hence you get the same DataTable
but revised structure
ReferenceURL : https://stackoverflow.com/questions/2949858/how-to-copy-only-the-columns-in-a-datatable-to-another-datatable
'UFO ET IT' 카테고리의 다른 글
프로그래밍 방식으로 C ++ 클래스 이름 검색 (0) | 2021.01.10 |
---|---|
DataView에서 행 반복 (0) | 2021.01.10 |
DEBUG 모드에서 NSLog 활성화 및 비활성화 (0) | 2021.01.10 |
MySQL 여러 행을 반환하는 SELECT 하위 쿼리를 사용하여 테이블에 어떻게 INSERT합니까? (0) | 2021.01.10 |
Google Play의 다운로드 수 (0) | 2021.01.10 |