using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable coustomertable = new DataTable("Customer");
coustomertable.Columns.Add("CustomerID",typeof (int));
coustomertable.Columns.Add("Name");
coustomertable.Columns.Add("TelePhone");
coustomertable.Columns.Add("Address");
//
DataRow cRow1 = coustomertable.NewRow() ;
cRow1[0] = 101;
cRow1[1] = "王为";
cRow1[2] = "0208344569";
cRow1[3] = "北京";
coustomertable.Rows.Add(cRow1 );
DataRow cRow2 = coustomertable.NewRow();
cRow2[0] = 102;
cRow2[1] = "李力";
cRow2[2] = "07568524456";
cRow2[3] = "广州";
coustomertable.Rows.Add(cRow2);
DataRow cRow3 = coustomertable.NewRow();
cRow3[0] = 103;
cRow3[1] = "胡总";
cRow3[2] = "0734810235";
cRow3[3] = "长沙";
coustomertable.Rows.Add(cRow3);
//
DataTable Orderstable = new DataTable("Orderstable");
Orderstable.Columns.Add("OrdersID",typeof (int ));
Orderstable.Columns.Add("CustomerID",typeof (int));
Orderstable.Columns.Add("Prices",typeof (int));
Orderstable.Columns.Add("ProduceName");
DataRow orow1 = Orderstable .NewRow ();
orow1[0] = 1;
orow1[1] = 101;
orow1[2] = 1500;
orow1[3] = "洗衣机";
Orderstable.Rows.Add(orow1 );
DataRow orow2 = Orderstable.NewRow();
orow2[0] = 2;
orow2[1] = 101;
orow2[2] = 2800;
orow2[3] = "冰箱";
Orderstable.Rows.Add(orow2);
DataRow orow3 = Orderstable.NewRow();
orow3[0] = 3;
orow3[1] = 102;
orow3[2] = 2000;
orow3[3] = "电视机";
Orderstable.Rows.Add(orow3);
DataRow orow4 = Orderstable.NewRow();
orow4[0] = 4;
orow4[1] = 101;
orow4[2] = 600;
orow4[3] = "电风扇";
Orderstable.Rows.Add(orow4);
DataRow orow5 = Orderstable.NewRow();
orow5[0] = 5;
orow5[1] = 102;
orow5[2] = 1000;
orow5[3] = "微波炉";
Orderstable.Rows.Add(orow5);
DataSet dataSet = new DataSet();
dataSet.Tables.Add(coustomertable );
dataSet.Tables.Add(Orderstable );
DataRelation coRelation=new DataRelation(" Customertoorders",coustomertable .Columns[0],Orderstable .Columns ["CustomerID"]);
dataSet.Relations.Add(coRelation );
foreach (DataRow row in coustomertable .Rows )
{
Console.WriteLine(row [1]+"`````````");
foreach (DataRow row1 in row.GetChildRows(coRelation ))
{
for (int i = 0; i < Orderstable.Columns.Count; i++)
{
Console.Write(row1[i] + " ");
}
Console.WriteLine();
}
}
Console.Read();
}
}
}
沒有留言:
張貼留言