Datatable learning
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5
6 namespace DatatablesColumnsRows
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 DataTable dt = new DataTable();
13
14 //code format 01
15 DataColumn dc01 = new DataColumn("StrVal", typeof(string));
16 dt.Columns.Add(dc01);
17
18 //code format 02
19 DataColumn dc02 = new DataColumn();
20 dc02.ColumnName = "IntVal";
21 dc02.DataType = typeof(int);
22 dt.Columns.Add(dc02);
23
24 DataColumn dc03 = new DataColumn();
25 dc03.DataType = System.Type.GetType("System.Decimal");
26 dc03.AllowDBNull = false;
27 dc03.Caption = "DclVal";
28 dc03.ColumnName = "DclVal";
29 dc03.DefaultValue = 1;
30 dt.Columns.Add(dc03);
31
32 //code format 03
33 dt.Columns.Add("ObjVal");
34
35 //code format 04
36 dt.Columns.Add("DTimeVal", typeof(DateTime));
37
38 //code format new row.
39 DataRow dr = dt.NewRow();
40 dr["StrVal"] = "AAA";
41 dr["DclVal"] = -123456789.0123;
42 dr["IntVal"] = 123;
43 dr["ObjVal"] = 123.456;
44 dr["DTimeVal"] = DateTime.Now;
45 dt.Rows.Add(dr);
46
47 DataRow r;
48 for (int i = 0; i < 10; i++)
49 {
50 r = dt.NewRow();
51 r["IntVal"] = i + 1;
52
53 // Be sure to add the new row to the
54 // DataRowCollection.
55 dt.Rows.Add(r);
56 }
57
58 Console.WriteLine("---------show value by rows-----------------");
59 foreach (DataRow row in dt.Rows)
60 {
61 foreach (DataColumn column in dt.Columns)
62 {
63 Console.WriteLine(row[column]);
64 }
65 }
66
67 Console.WriteLine("---------show value by row count-----------------");
68 for (int i = 0; i < dt.Rows.Count; i++)
69 {
70 Console.WriteLine(dt.Columns[0].ToString() + " - " + dt.Rows[i]["StrVal"].ToString());
71 Console.WriteLine(dt.Columns[1].ToString() + " - " + dt.Rows[i]["IntVal"].ToString());
72 Console.WriteLine(dt.Columns[2].ToString() + " - " + dt.Rows[i]["DclVal"].ToString());
73 Console.WriteLine(dt.Columns[3].ToString() + " - " + dt.Rows[i]["ObjVal"].ToString());
74 Console.WriteLine(dt.Columns[4].ToString() + " - " + dt.Rows[i]["DTimeVal"].ToString());
75 }
76
77 Console.WriteLine("---------show value by DataColumnCollection------");
78 DataColumnCollection clms = dt.Columns;
79 foreach (DataColumn column in clms)
80 {
81 Console.WriteLine("Column Name: " + column.ColumnName);
82 Console.WriteLine("Column Type: " + column.DataType);
83 }
84
85 Console.WriteLine("---------copy table------");
86 DataTable dtCopy = dt.Copy();
87 foreach (DataRow tempDr in dtCopy.Rows)
88 {
89 Console.WriteLine(tempDr[0].ToString());
90 Console.WriteLine(tempDr[1].ToString());
91 Console.WriteLine(tempDr[2].ToString());
92 Console.WriteLine(tempDr[3].ToString());
93 Console.WriteLine(tempDr[4].ToString());
94 }
95
96
97 Console.WriteLine("---------clone table------");
98 DataTable dtClone = dt.Clone();
99 foreach (DataRow tmpDr in dtClone.Rows)
100 {
101 Console.WriteLine(tmpDr[0].ToString());
102 Console.WriteLine(tmpDr[1].ToString());
103 Console.WriteLine(tmpDr[2].ToString());
104 Console.WriteLine(tmpDr[3].ToString());
105 Console.WriteLine(tmpDr[4].ToString());
106 }
107 DataColumnCollection clms2 = dtClone.Columns;
108 foreach (DataColumn column2 in clms2)
109 {
110 Console.WriteLine("Column Name: " + column2.ColumnName);
111 Console.WriteLine("Column Type: " + column2.DataType);
112 }
113
114
115 DataRow row2;
116 int RowCount = dtClone.Rows.Count;
117 int ColCount = dtClone.Rows.Count;
118 for (int rowIndex = 1; rowIndex <= RowCount; rowIndex++)
119 {
120 row2 = dtClone.NewRow();
121 for (int i = 1; i <= ColCount; i++)
122 {
123 row2["ColString" + i.ToString()] = string.Concat(rowIndex.ToString(), ".", i.ToString());
124 row2["ColInt" + i.ToString()] = rowIndex * 1000 + i;
125 }
126 dtClone.Rows.Add(row2);
127 }
128
129 foreach (DataRow tmpDr in dtClone.Rows)
130 {
131 Console.WriteLine(tmpDr[0].ToString());
132 Console.WriteLine(tmpDr[1].ToString());
133 Console.WriteLine(tmpDr[2].ToString());
134 Console.WriteLine(tmpDr[3].ToString());
135 Console.WriteLine(tmpDr[4].ToString());
136 }
137
138 Console.ReadLine();
139
140 }
141 }
142 }
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5
6 namespace DatatablesColumnsRows
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 DataTable dt = new DataTable();
13
14 //code format 01
15 DataColumn dc01 = new DataColumn("StrVal", typeof(string));
16 dt.Columns.Add(dc01);
17
18 //code format 02
19 DataColumn dc02 = new DataColumn();
20 dc02.ColumnName = "IntVal";
21 dc02.DataType = typeof(int);
22 dt.Columns.Add(dc02);
23
24 DataColumn dc03 = new DataColumn();
25 dc03.DataType = System.Type.GetType("System.Decimal");
26 dc03.AllowDBNull = false;
27 dc03.Caption = "DclVal";
28 dc03.ColumnName = "DclVal";
29 dc03.DefaultValue = 1;
30 dt.Columns.Add(dc03);
31
32 //code format 03
33 dt.Columns.Add("ObjVal");
34
35 //code format 04
36 dt.Columns.Add("DTimeVal", typeof(DateTime));
37
38 //code format new row.
39 DataRow dr = dt.NewRow();
40 dr["StrVal"] = "AAA";
41 dr["DclVal"] = -123456789.0123;
42 dr["IntVal"] = 123;
43 dr["ObjVal"] = 123.456;
44 dr["DTimeVal"] = DateTime.Now;
45 dt.Rows.Add(dr);
46
47 DataRow r;
48 for (int i = 0; i < 10; i++)
49 {
50 r = dt.NewRow();
51 r["IntVal"] = i + 1;
52
53 // Be sure to add the new row to the
54 // DataRowCollection.
55 dt.Rows.Add(r);
56 }
57
58 Console.WriteLine("---------show value by rows-----------------");
59 foreach (DataRow row in dt.Rows)
60 {
61 foreach (DataColumn column in dt.Columns)
62 {
63 Console.WriteLine(row[column]);
64 }
65 }
66
67 Console.WriteLine("---------show value by row count-----------------");
68 for (int i = 0; i < dt.Rows.Count; i++)
69 {
70 Console.WriteLine(dt.Columns[0].ToString() + " - " + dt.Rows[i]["StrVal"].ToString());
71 Console.WriteLine(dt.Columns[1].ToString() + " - " + dt.Rows[i]["IntVal"].ToString());
72 Console.WriteLine(dt.Columns[2].ToString() + " - " + dt.Rows[i]["DclVal"].ToString());
73 Console.WriteLine(dt.Columns[3].ToString() + " - " + dt.Rows[i]["ObjVal"].ToString());
74 Console.WriteLine(dt.Columns[4].ToString() + " - " + dt.Rows[i]["DTimeVal"].ToString());
75 }
76
77 Console.WriteLine("---------show value by DataColumnCollection------");
78 DataColumnCollection clms = dt.Columns;
79 foreach (DataColumn column in clms)
80 {
81 Console.WriteLine("Column Name: " + column.ColumnName);
82 Console.WriteLine("Column Type: " + column.DataType);
83 }
84
85 Console.WriteLine("---------copy table------");
86 DataTable dtCopy = dt.Copy();
87 foreach (DataRow tempDr in dtCopy.Rows)
88 {
89 Console.WriteLine(tempDr[0].ToString());
90 Console.WriteLine(tempDr[1].ToString());
91 Console.WriteLine(tempDr[2].ToString());
92 Console.WriteLine(tempDr[3].ToString());
93 Console.WriteLine(tempDr[4].ToString());
94 }
95
96
97 Console.WriteLine("---------clone table------");
98 DataTable dtClone = dt.Clone();
99 foreach (DataRow tmpDr in dtClone.Rows)
100 {
101 Console.WriteLine(tmpDr[0].ToString());
102 Console.WriteLine(tmpDr[1].ToString());
103 Console.WriteLine(tmpDr[2].ToString());
104 Console.WriteLine(tmpDr[3].ToString());
105 Console.WriteLine(tmpDr[4].ToString());
106 }
107 DataColumnCollection clms2 = dtClone.Columns;
108 foreach (DataColumn column2 in clms2)
109 {
110 Console.WriteLine("Column Name: " + column2.ColumnName);
111 Console.WriteLine("Column Type: " + column2.DataType);
112 }
113
114
115 DataRow row2;
116 int RowCount = dtClone.Rows.Count;
117 int ColCount = dtClone.Rows.Count;
118 for (int rowIndex = 1; rowIndex <= RowCount; rowIndex++)
119 {
120 row2 = dtClone.NewRow();
121 for (int i = 1; i <= ColCount; i++)
122 {
123 row2["ColString" + i.ToString()] = string.Concat(rowIndex.ToString(), ".", i.ToString());
124 row2["ColInt" + i.ToString()] = rowIndex * 1000 + i;
125 }
126 dtClone.Rows.Add(row2);
127 }
128
129 foreach (DataRow tmpDr in dtClone.Rows)
130 {
131 Console.WriteLine(tmpDr[0].ToString());
132 Console.WriteLine(tmpDr[1].ToString());
133 Console.WriteLine(tmpDr[2].ToString());
134 Console.WriteLine(tmpDr[3].ToString());
135 Console.WriteLine(tmpDr[4].ToString());
136 }
137
138 Console.ReadLine();
139
140 }
141 }
142 }