달력

022012  이전 다음

  •  
  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  •  
  •  
  •  
DataTables 간에 DataRows를 복사하는 방법

ImportRow 메서드를 사용하기 전에 대상 테이블의 구조가 원본 테이블과 동일한지 확인해야 합니다.
이 예제에서는 DataTable 클래스의 Clone 메서드를 사용하여 모든 DataTable 스키마, 관계, 제약 조건뿐 아니라 DataTable의 구조도 복사합니다.

// 원본 객체로 DB 가져온 데이터를 매핑 시켜줍니다. (생략)
DataTable tblProducts = new  DataTable();
// 원본 객체에서 복사할 객체입니다.
DataTable tblProductsCopy = new DataTable();
// 복사용 객체에 스키마, 관계, 제약 조건들을 원본 객체와 같게 적용 합니다.
tblProductsCopy = tblProducts.Clone();
for (i=0; i<=4;++i)
{
    // 원본 객체에서 복사를 실행합니다.
    tblProductsCopy.ImportRow(tblProducts.Rows[i]);
}

추가1. DataTable 개체의 Copy 메서드를 사용하여 전체 DataTable을 복사할 수 있습니다.

DataTable DataTable1 = new DataTable();
DataTable DataTable2 = new DataTable();
// DataTable의 Copy 메서드를 이용하여 복사한다.
DataTable2 = DataTable1.Copy();

추가2. 필터링된 DataView 클래스의 결과나 Select 메서드의 결과에서도 DataRow 개체를 복사할 수 있습니다.

// Select 메서드를 사용하여 결과값을 복사한다.
foreach (DataRow MyDataRow in DataTable1.Select("Region = 'WA'"))
{
    DataTable2.ImportRow(MyDataRow);
}
// DataView의 필터 기능을 사용하여 결과값을 복사한다.
DataView1 = DataTable1.DefaultView;
DataView1.RowFilter = "Region = 'WA'";
for (int i = 0; i <= DataView1.Count - 1; ++i)
{
    DataTable2.ImportRow(DataView1[I].Row);
}

작성자 : 상현넘™ [SHBLITZ.NET]
참고 : http://support.microsoft.com/default.aspx?scid=kb;KO;308909
Posted by 상현넘™

댓글을 달아 주세요