Quick Objects Documentation Akal Tech Logo
Delete Multiple Records

Glossary Item Box

Business Logic Framework - How To:

Deleting multiple records at a time is very simple task with Quick Objects. The process is similar to specifying a search criteria for Find method.  Once the search criteria is specified calling the BulkDelete method performs the actual delete. For search criteria you can even specify values for "joined" objects and only a single SQL statement is produced and used by the Business Logic Framework to achieve the desired results.

 Note: We recommend that you use caution while using BulkUpdate and BulkDelete methods as they can modify/delete many records at a time, and if wrong criteria is specified by mistake the result can be disastrous.

Target of the following code is to delete orders that are older than 60 days.

Example 1: Delete record with search criteria in single table.

1 using (Orders o = new Orders())  
2 {  
3     o.ObjectMode = ObjectModes.Search;  
4     o.OrderDate.Value = DateTime.UtcNow.AddDays(-60);  
5     o.OrderDate.Operator = Operators.LessThan;  
6     if (o.BulkDelete())  
7     {  
8         MessageBox.Show(o.AffectedRecords.ToString() + " orders older than 60 days have been deleted!");  
9     }  
10     else 
11     {  
12         MessageBox.Show("Old orders could not be deleted. Error: " + o.ErrorString);  
13     }  
14 }  
15  
1 Using o As Orders = New Orders()  
2     o.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search  
3     o.OrderDate.Value = DateTime.UtcNow.AddDays(-60)  
4     o.OrderDate.Operator = Operators.LessThan  
5     If o.BulkDelete() Then 
6         MessageBox.Show(o.AffectedRecords.ToString() & " orders older than 60 days have been deleted!")  
7     Else 
8         MessageBox.Show("Old orders could not be deleted. Error: " & o.ErrorString)  
9     End If 
10 End Using  
11  

 

Example 2: Delete multiple records by specifying search criteria on multiple related tables.

1 // In this case we are deleting all orders placed by customers that have been a customer for longer than 6 months.  
2 // Also if the orders contain any item whose value is greater than 100.  
3 using (Orders o = new Orders())  
4 {  
5     o.ObjectMode = ObjectModes.Search;  
6     o.CreateJoinWith_CustomerID_Customers_Parent_Obj();  
7     o.CustomerID_Customers_Parent_Obj.CustomerSince.Value = DateTime.UtcNow.AddMonths(-6);  
8     o.CustomerID_Customers_Parent_Obj.CustomerSince.Operator = Operators.LessThan;  
9     o.CreateJoinWith_OrderID_OrderItems_Child_Obj();  
10     o.OrderID_OrderItems_Child_Obj.ItemAmount.Value = 100;  
11     o.OrderID_OrderItems_Child_Obj.ItemAmount.Operator = Operators.GreaterThan;  
12     if (o.BulkDelete())  
13     {  
14         MessageBox.Show(o.AffectedRecords.ToString() + " orders older than 60 days have been deleted!");  
15     }  
16     else 
17     {  
18         MessageBox.Show("Old orders could not be deleted. Error: " + o.ErrorString);  
19     }  
20 }  
21  
1 ' In this case we are deleting all orders placed by customers that have been a customer for longer than 6 months.  
2 ' Also if the orders contain any item whose value is greater than 100.  
3 Using o As Orders = New Orders()  
4     o.ObjectMode = ObjectModes.Search  
5     o.Join_CustomerID_Customers_Parent()  
6     o.CustomerID_Customers_Parent.CustomerSince.Value = DateTime.UtcNow.AddMonths(-6)  
7     o.CustomerID_Customers_Parent.CustomerSince.Operator = Operators.LessThan  
8     o.Join_OrderID_OrderItems_Child()  
9     o.OrderID_OrderItems_Child.ItemAmount.Value = 100  
10     o.OrderID_OrderItems_Child.ItemAmount.Operator = Operators.GreaterThan  
11     If o.BulkDelete() Then 
12         MessageBox.Show(o.AffectedRecords.ToString() & " orders older than 60 days have been deleted!")  
13     Else 
14         MessageBox.Show("Old orders could not be deleted. Error: " & o.ErrorString)  
15     End If 
16 End Using  
17  
 

See also:

BulkDelete Method