// Adds random employee data to the database by using dataflow. // This method is similar to AddEmployees except that it uses batching // to add multiple employees to the database at a time. static void AddEmployeesBatched(string connectionString, int batchSize, int count) { // Create a BatchBlock<Employee> that holds several Employee objects and // then propagates them out as an array. BatchBlock <Employee> batchEmployees = new BatchBlock <Employee>(batchSize); // Create an ActionBlock<Employee[]> object that adds multiple // employee entries to the database. ActionBlock <Employee[]> insertEmployees = new ActionBlock <Employee[]>(a => { DatabaseUtilities.InsertEmployees(a, connectionString, "AddEmployeesBatched"); }); // Link the batch block to the action block. batchEmployees.LinkTo(insertEmployees); // When the batch block completes, set the action block also to complete. batchEmployees.Completion.ContinueWith(obj => { insertEmployees.Complete(); }); // Post several random Employee objects to the batch block. PostRandomEmployees(batchEmployees, count); // Set the batch block to the completed state and wait for // all insert operations to complete. batchEmployees.Complete(); insertEmployees.Completion.Wait(); }
// Adds random employee data to the database by using dataflow. static void AddEmployees(string connectionString, int count) { // Create an ActionBlock<Employee> object that adds a single // employee entry to the database. ActionBlock <Employee> insertEmployee = new ActionBlock <Employee>(e => { DatabaseUtilities.InsertEmployees(new[] { e }, connectionString, "AddEmployees"); }); // Post several random Employee objects to the dataflow block. PostRandomEmployees(insertEmployee, count); // Set the dataflow block to the completed state and wait for // all insert operations to complete. insertEmployee.Complete(); insertEmployee.Completion.Wait(); }