DataSet, DataTable, GetXML예제
꽁스짱
C#
0
1145
2021.02.15 22:58
DataSet, DataTable, GetXML예제
using System;
using System.Data;
class DTExample
{
static void Main()
{
DataTable dt = new DataTable("TableExample");
DataColumn col1 = new DataColumn(); DataColumn col2 = new DataColumn();
DataColumn col3 = new DataColumn(); DataColumn col4 = new DataColumn();
col1.DataType = System.Type.GetType("System.Int16");
col1.ReadOnly = true; col1.AllowDBNull = false; col1.Unique = true;
col1.ColumnName = "ID"; col1.AutoIncrement = true; col1.AutoIncrementSeed = 1;
col2.DataType = System.Type.GetType("System.String"); col2.ColumnName = "Name";
col3.DataType = System.Type.GetType("System.String"); col3.ColumnName = "Addr";
col3.DefaultValue = "서울";
col4.DataType = System.Type.GetType("System.String");
col4.ColumnName = "Tel";
dt.Columns.Add(col1); dt.Columns.Add(col2);
dt.Columns.Add(col3); dt.Columns.Add(col4);
DataRow row1 = dt.NewRow();
row1[1] = "가길동"; row1[2] = "수웎"; row1[3] = "111-2222";
dt.Rows.Add(row1);
DataRow row2 = dt.NewRow();
row2[1] = "나길동"; row2[2] = "울산"; row2[3] = "111-2222";
dt.Rows.Add(row2);
DataRow row3 = dt.NewRow();
row3[1] = "다길동"; row3[2] = "부산"; row3[3] = "333-2222";
dt.Rows.Add(row3);
Console.Write("\n");
foreach (DataColumn header in dt.Columns)
{
Console.Write("{0, -10}\t", header.ColumnName);
}
Console.WriteLine("\n");
foreach (DataRow rows in dt.Rows)
{
foreach (DataColumn cols in dt.Columns)
{
Console.Write("{0, -10}\t", rows[cols.ColumnName]);
}
Console.Write("\n");
}
Console.WriteLine("\n"); Console.WriteLine("Done!, Press Enter.");
DataSet set = new DataSet("office");
set.Tables.Add(dt);
Console.WriteLine(set.GetXml());
Console.WriteLine("\n"); Console.ReadLine();
}
}
[결과]
ID Name Addr Tel
1 가길동 수웎 111-2222
2 나길동 울산 111-2222
3 다길동 부산 333-2222
Done!, Press Enter.
<office>
<TableExample>
<ID>1</ID>
<Name>가길동</Name>
<Addr>수웎</Addr>
<Tel>111-2222</Tel>
</TableExample>
<TableExample>
<ID>2</ID>
<Name>나길동</Name>
<Addr>울산</Addr>
<Tel>111-2222</Tel>
</TableExample>
<TableExample>
<ID>3</ID>
<Name>다길동</Name>
<Addr>부산</Addr>
<Tel>333-2222</Tel>
</TableExample>
</office>