|
| 1 | +// <Snippet1> |
| 2 | +using System; |
| 3 | +using System.Data; |
| 4 | + |
| 5 | +class Program |
| 6 | +{ |
| 7 | + static void Main(string[] args) |
| 8 | + { |
| 9 | + // Create two tables and add them into the DataSet |
| 10 | + DataTable orderTable = CreateOrderTable(); |
| 11 | + DataTable orderDetailTable = CreateOrderDetailTable(); |
| 12 | + DataSet salesSet = new DataSet(); |
| 13 | + salesSet.Tables.Add(orderTable); |
| 14 | + salesSet.Tables.Add(orderDetailTable); |
| 15 | + |
| 16 | + // Set the relations between the tables and create the related constraint. |
| 17 | + salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns["OrderId"], orderDetailTable.Columns["OrderId"], true); |
| 18 | + |
| 19 | + Console.WriteLine("After creating the foreign key constriant, you will see the following error if inserting order detail with the wrong OrderId: "); |
| 20 | + try |
| 21 | + { |
| 22 | + DataRow errorRow = orderDetailTable.NewRow(); |
| 23 | + errorRow[0] = 1; |
| 24 | + errorRow[1] = "O0007"; |
| 25 | + orderDetailTable.Rows.Add(errorRow); |
| 26 | + } |
| 27 | + catch (Exception e) |
| 28 | + { |
| 29 | + Console.WriteLine(e.Message); |
| 30 | + } |
| 31 | + Console.WriteLine(); |
| 32 | + |
| 33 | + // Insert the rows into the table |
| 34 | + InsertOrders(orderTable); |
| 35 | + InsertOrderDetails(orderDetailTable); |
| 36 | + |
| 37 | + Console.WriteLine("The initial Order table."); |
| 38 | + ShowTable(orderTable); |
| 39 | + |
| 40 | + Console.WriteLine("The OrderDetail table."); |
| 41 | + ShowTable(orderDetailTable); |
| 42 | + |
| 43 | + // Use the Aggregate-Sum on the child table column to get the result. |
| 44 | + DataColumn colSub = new DataColumn("SubTotal", typeof(Decimal), "Sum(Child.LineTotal)"); |
| 45 | + orderTable.Columns.Add(colSub); |
| 46 | + |
| 47 | + // Compute the tax by referencing the SubTotal expression column. |
| 48 | + DataColumn colTax = new DataColumn("Tax", typeof(Decimal), "SubTotal*0.1"); |
| 49 | + orderTable.Columns.Add(colTax); |
| 50 | + |
| 51 | + // If the OrderId is 'Total', compute the due on all orders; or compute the due on this order. |
| 52 | + DataColumn colTotal = new DataColumn("TotalDue", typeof(Decimal), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)"); |
| 53 | + orderTable.Columns.Add(colTotal); |
| 54 | + |
| 55 | + DataRow row = orderTable.NewRow(); |
| 56 | + row["OrderId"] = "Total"; |
| 57 | + orderTable.Rows.Add(row); |
| 58 | + |
| 59 | + Console.WriteLine("The Order table with the expression columns."); |
| 60 | + ShowTable(orderTable); |
| 61 | + |
| 62 | + Console.WriteLine("Press any key to exit....."); |
| 63 | + Console.ReadKey(); |
| 64 | + } |
| 65 | + |
| 66 | + private static DataTable CreateOrderTable() |
| 67 | + { |
| 68 | + DataTable orderTable = new DataTable("Order"); |
| 69 | + |
| 70 | + // Define one column. |
| 71 | + DataColumn colId = new DataColumn("OrderId", typeof(String)); |
| 72 | + orderTable.Columns.Add(colId); |
| 73 | + |
| 74 | + DataColumn colDate = new DataColumn("OrderDate", typeof(DateTime)); |
| 75 | + orderTable.Columns.Add(colDate); |
| 76 | + |
| 77 | + // Set the OrderId column as the primary key. |
| 78 | + orderTable.PrimaryKey = new DataColumn[] { colId }; |
| 79 | + |
| 80 | + return orderTable; |
| 81 | + } |
| 82 | + |
| 83 | + private static DataTable CreateOrderDetailTable() |
| 84 | + { |
| 85 | + DataTable orderDetailTable = new DataTable("OrderDetail"); |
| 86 | + |
| 87 | + // Define all the columns once. |
| 88 | + DataColumn[] cols = |
| 89 | + { |
| 90 | + new DataColumn("OrderDetailId", typeof(Int32)), |
| 91 | + new DataColumn("OrderId", typeof(String)), |
| 92 | + new DataColumn("Product", typeof(String)), |
| 93 | + new DataColumn("UnitPrice", typeof(Decimal)), |
| 94 | + new DataColumn("OrderQty", typeof(Int32)), |
| 95 | + new DataColumn("LineTotal", typeof(Decimal), "UnitPrice*OrderQty") |
| 96 | + }; |
| 97 | + |
| 98 | + orderDetailTable.Columns.AddRange(cols); |
| 99 | + orderDetailTable.PrimaryKey = new DataColumn[] { orderDetailTable.Columns["OrderDetailId"] }; |
| 100 | + return orderDetailTable; |
| 101 | + } |
| 102 | + |
| 103 | + private static void InsertOrders(DataTable orderTable) |
| 104 | + { |
| 105 | + // Add one row once. |
| 106 | + DataRow row1 = orderTable.NewRow(); |
| 107 | + row1["OrderId"] = "O0001"; |
| 108 | + row1["OrderDate"] = new DateTime(2013, 3, 1); |
| 109 | + orderTable.Rows.Add(row1); |
| 110 | + |
| 111 | + DataRow row2 = orderTable.NewRow(); |
| 112 | + row2["OrderId"] = "O0002"; |
| 113 | + row2["OrderDate"] = new DateTime(2013, 3, 12); |
| 114 | + orderTable.Rows.Add(row2); |
| 115 | + |
| 116 | + DataRow row3 = orderTable.NewRow(); |
| 117 | + row3["OrderId"] = "O0003"; |
| 118 | + row3["OrderDate"] = new DateTime(2013, 3, 20); |
| 119 | + orderTable.Rows.Add(row3); |
| 120 | + } |
| 121 | + |
| 122 | + private static void InsertOrderDetails(DataTable orderDetailTable) |
| 123 | + { |
| 124 | + // Use an Object array to insert all the rows . |
| 125 | + // Values in the array are matched sequentially to the columns, based on the order in which they appear in the table. |
| 126 | + Object[] rows = |
| 127 | + { |
| 128 | + new Object[] { 1, "O0001", "Mountain Bike", 1419.5, 36 }, |
| 129 | + new Object[] { 2, "O0001", "Road Bike", 1233.6, 16 }, |
| 130 | + new Object[] { 3, "O0001", "Touring Bike", 1653.3, 32 }, |
| 131 | + new Object[] { 4, "O0002", "Mountain Bike", 1419.5, 24 }, |
| 132 | + new Object[] { 5, "O0002", "Road Bike", 1233.6, 12 }, |
| 133 | + new Object[] { 6, "O0003", "Mountain Bike", 1419.5, 48 }, |
| 134 | + new Object[] { 7, "O0003", "Touring Bike", 1653.3, 8 }, |
| 135 | + }; |
| 136 | + |
| 137 | + foreach (Object[] row in rows) |
| 138 | + { |
| 139 | + orderDetailTable.Rows.Add(row); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static void ShowTable(DataTable table) |
| 144 | + { |
| 145 | + foreach (DataColumn col in table.Columns) |
| 146 | + { |
| 147 | + Console.Write("{0,-14}", col.ColumnName); |
| 148 | + } |
| 149 | + Console.WriteLine(); |
| 150 | + |
| 151 | + foreach (DataRow row in table.Rows) |
| 152 | + { |
| 153 | + foreach (DataColumn col in table.Columns) |
| 154 | + { |
| 155 | + if (col.DataType.Equals(typeof(DateTime))) |
| 156 | + Console.Write("{0,-14:d}", row[col]); |
| 157 | + else if (col.DataType.Equals(typeof(Decimal))) |
| 158 | + Console.Write("{0,-14:C}", row[col]); |
| 159 | + else |
| 160 | + Console.Write("{0,-14}", row[col]); |
| 161 | + } |
| 162 | + Console.WriteLine(); |
| 163 | + } |
| 164 | + Console.WriteLine(); |
| 165 | + } |
| 166 | +} |
| 167 | +// </Snippet1> |
0 commit comments