DataTable举例

 1 // clrTest1.cpp: 主项目文件。

 2 

 3 #include "stdafx.h"

 4 

 5 using namespace System;

 6 using namespace System::Data;

 7 

 8 int main(array<System::String ^> ^args)

 9 {

10     DataTable^ dtable = gcnew DataTable("myTable");

11     DataColumn^ column;

12     //col 1

13     column = gcnew DataColumn("begin_time",System::DateTime::typeid);  

14     dtable->Columns->Add(column);

15     // col 2

16     column = gcnew DataColumn("name",  String::typeid); 

17     dtable->Columns->Add(column);

18     // col 3

19     column = gcnew DataColumn("dscrib",  String::typeid); 

20     dtable->Columns->Add(column);

21     //primary key

22     array<DataColumn^>^ Keys = gcnew array<DataColumn^>(2); 

23     Keys[0] = dtable->Columns["name"];

24     Keys[1] = dtable->Columns["dscrib"];

25     dtable->PrimaryKey = Keys;

26     //dataset 

27     DataSet^ dset  = gcnew DataSet();

28     dset->Tables->Add(dtable);

29     //add dataRow

30     String^ str = Console::ReadLine();

31     int lines ;

32     try

33     {

34         lines = Convert::ToInt32(str);

35     }

36     catch (...){} 

37     DataRow^ row ;

38     for(int i = 0 ; i < lines ; i ++)

39     {

40         row = dtable->NewRow();

41         DateTime^ dtime  = DateTime::Now; 

42         dtime = dtime->AddHours(i);

43        row["begin_time"] = dtime ;

44        row["name"] = "John_"+(i+1);

45        row["dscrib"] = "it_is_row_"+(i+1);

46        dtable->Rows->Add(row);

47     }

48     //out-put

49     for(int i = 0 ; i < dtable->Rows->Count; i ++)

50     {

51         row = dtable->Rows[i];

52         for(int j = 0 ; j < dtable->Columns->Count; j ++)

53         {

54             Console::WriteLine(row[j]+"\t");

55         }

56         Console::WriteLine();

57     }

58     Console::ReadKey();

59     return 0;

60 }

DataTable举例

你可能感兴趣的:(Datatable)