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