static void Main() { Random randomGen = new Random(); for (int i = 0; i < 2500; i++) { // Create a client with given client endpoint configuration OrderProcessorClient client = new OrderProcessorClient("OrderProcessorEndpoint"); // Create the purchase order PurchaseOrder po = new PurchaseOrder(); po.CustomerId = "somecustomer" + i + ".com"; po.PONumber = Guid.NewGuid().ToString(); PurchaseOrderLineItem lineItem1 = new PurchaseOrderLineItem(); lineItem1.ProductId = "Blue Widget"; lineItem1.Quantity = randomGen.Next(1, 100); lineItem1.UnitCost = (float)randomGen.NextDouble() * 10; PurchaseOrderLineItem lineItem2 = new PurchaseOrderLineItem(); lineItem2.ProductId = "Red Widget"; lineItem2.Quantity = 890; lineItem2.UnitCost = 45.89F; po.orderLineItems = new PurchaseOrderLineItem[2]; po.orderLineItems[0] = lineItem1; po.orderLineItems[1] = lineItem2; //Create a transaction scope. using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { // Make a queued call to submit the purchase order client.SubmitPurchaseOrder(po); // Complete the transaction. scope.Complete(); } client.Close(); } Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); }
public void SubmitPurchaseOrder(PurchaseOrder po) { Console.WriteLine("Processing {0} ", po); Orders.Add(po); }
public static void Add(PurchaseOrder po) { // insert purchase order SqlCommand insertPurchaseOrderCommand = new SqlCommand("insert into PurchaseOrders(poNumber, customerId) values(@poNumber, @customerId)"); insertPurchaseOrderCommand.Parameters.Add("@poNumber", SqlDbType.VarChar, 50); insertPurchaseOrderCommand.Parameters.Add("@customerId", SqlDbType.VarChar, 50); // insert product line item SqlCommand insertProductLineItemCommand = new SqlCommand("insert into ProductLineItems(productId, unitCost, quantity, poNumber) values(@productId, @unitCost, @quantity, @poNumber)"); insertProductLineItemCommand.Parameters.Add("@productId", SqlDbType.VarChar, 50); insertProductLineItemCommand.Parameters.Add("@unitCost", SqlDbType.Float); insertProductLineItemCommand.Parameters.Add("@quantity", SqlDbType.Int); insertProductLineItemCommand.Parameters.Add("@poNumber", SqlDbType.VarChar, 50); int rowsAffected = 0; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["connectionString"])) { conn.Open(); // insert into purchase order table insertPurchaseOrderCommand.Connection = conn; insertPurchaseOrderCommand.Parameters["@poNumber"].Value = po.PONumber; insertPurchaseOrderCommand.Parameters["@customerId"].Value = po.CustomerId; insertPurchaseOrderCommand.ExecuteNonQuery(); // insert into product line item table insertProductLineItemCommand.Connection = conn; foreach (PurchaseOrderLineItem orderLineItem in po.orderLineItems) { insertProductLineItemCommand.Parameters["@poNumber"].Value = po.PONumber; insertProductLineItemCommand.Parameters["@productId"].Value = orderLineItem.ProductId; insertProductLineItemCommand.Parameters["@unitCost"].Value = orderLineItem.UnitCost; insertProductLineItemCommand.Parameters["@quantity"].Value = orderLineItem.Quantity; rowsAffected += insertProductLineItemCommand.ExecuteNonQuery(); } scope.Complete(); } } Console.WriteLine("Updated database with {0} product line items for purchase order {1} ", rowsAffected, po.PONumber); }