Step by step, we will teach you how to use the AgileEAS. NET base class library for application development-basics-demonstrate batch deletion and update of ORM

Source: Internet
Author: User
Series Review

The previous article teaches you how to use AgileEAS step by step.. NET basic library for application development-basics-demonstrate basic operations of ORM and teach you how to use AgileEAS step by step. NET basic library for application development-basics-demonstrate ORM condition query two articles I demonstrated the basic usage of ORM and extended to the most common condition query service in development, today, I used the basic demonstration of conditional query in the previous article to batch Delete and update conditions.

Problem proposal

In the "Operation-based" section of the demo ORM, we demonstrated the deletion (Update) and Update (processing) based on a single record (entity), but did not mention the Update and deletion of a volume record, this article aims to propose a solution for An ORM application and demonstrate its usage.

If

The essence of batch Update and deletion of data records is to execute Update and Delete statements with batch query conditions. The ORM Component must complete such functions, the idea is to generate SQL statements for batch update based on the conditions. The new SQL statements for batch deletion are: delete + database table name + where + conditional expression, and the new SQL statements for batch update: update + database table name + set + (column name =? [,…]) + Where + conditional expression.

Now that we know the processing method, we can convert it to the representation of the ORM Component for processing. There are three methods in the ITable interface of the orm Component:

/// <Summary> /// delete a data table record from the database. /// </Summary> /// <param name = "condition"> Data Query condition. </Param> /// <remarks> the number of data rows affected by the delete operation. </Remarks> void Delete (Condition condition); // <summary> // update the value of a specified column in the database table based on the Condition. /// </Summary> /// <param name = "colValues"> the column to be updated and the specified value. </Param> /// <param name = "condition"> Data Query condition. </Param> /// <remarks> the number of data rows affected by the update operation. </Remarks> void Update (ColumnCollection colValues, Condition condition); // <summary> // Update the values of specified columns in the database table based on the conditions. /// </Summary> /// <param name = "colValues"> the column to be updated and the specified value. </Param> /// <param name = "condition"> Data Query condition. </Param> /// <remarks> the number of data rows affected by the update operation. </Remarks> void Update (IDictionary <string, object> colValues, Condition condition );

The Delete method is easy to use. It only needs to include the condition object for deleting the record. The use of the Update method is more complex. In addition to setting the condition for updating the record, you must set the content of the Update column, it provides two forms: ColumnCollection and dictionary set. The purpose is to complete the dictionary name =? (Value.

Introduction

The knowledge about conditional queries and condition construction is similar to that of these. There is no beginning to talk about how to combine these conditions to implement complex services, in today's case, I selectively demonstrate the usage of several conditions. The data used is the commodity dictionary, which provides the following functions:

1. Delete all items with a record ID ranging from 100 to 500, in the unit of "1 * bottle/bottle.

2. update the description of all items whose first two digits are "1a" and unit is "1 * bag/bag" as "spam ", this example uses two types of update overload.

Add the following code to the ProductList class of the ClassLibDemo. DAL. SQLServer project:

    public void DeleteProduct(int iStart, int iEnd, string unit)    {        Condition condition = this.CreateCondition();        List<int> values = new List<int>(2);        values.Add(iStart);        values.Add(iEnd);        condition.AddElement("IDN", values, ElementType.BetWeen);        condition.AddElement("UNIT", unit);        this.Delete(condition);    }    public void UpdateProductDescription(string code, string unit,string desc)    {        Condition condition = this.CreateCondition();        condition.AddElement("CODE", code, ElementType.MatchPrefix);        condition.AddElement("UNIT", unit);        Dictionary<string, object> values = new Dictionary<string, object>();        values.Add("DESCRIPTION", desc);        this.Update(values,condition);    }            public void UpdateProductDescription2(string code, string unit,string desc)    {        Condition condition = this.CreateCondition();        condition.AddElement("CODE", code, ElementType.MatchPrefix);        condition.AddElement("UNIT", unit);        ColumnCollection values = new ColumnCollection(this);        values.AddColumn("DESCRIPTION",DbType.String,desc);        this.Update(values,condition);    }        

We use these queries in the console Demonstration Project:

Class ConditionUpdate {/// <summary> /// delete all items with the record ID in the unit of "* bottle/bottle. /// </Summary> public void DemoConditionDelete () {ProductList table = new ProductList (); table. ormAccessor = OrmContext. ormAccessor; table. deleteProduct (100,500, "1 * bottle/bottle"); System. console. writeLine ("deleted ");} /// <summary> /// updated the description of all commodities whose first two digits are "1a" and whose unit is "1 * bag/bag" is "junk goods "". /// </Summary> public void DemoConditionUpdate () {ProductList table = new ProductList (); table. ormAccessor = OrmContext. ormAccessor; table. updateProductDescription ("1a", "1 * bags/bags", "junk goods"); System. console. writeLine ("updated ");} /// <summary> /// Delete and update all items whose first two digits are "1a" and whose unit is "1 * bag/bag" are described as "junk goods "". /// </Summary> public void DemoConditionUpdate2 () {ProductList table = new ProductList (); table. ormAccessor = OrmContext. ormAccessor; table. updateProductDescription2 ("1a", "1 * bag/bag", "junk goods"); System. console. writeLine ("updated ");}

Case output:

SQL event profiling:

For the structure of the data table involved in this example, refer to the data table structure based on AgileEAS. NET platform basic library for application development-General description and data definition, for data object model definition files, documents, DDL scripts download: Workshop.

Link

Step by step teach you how to use the AgileEAS. NET base class library for application development-series directory

AgileEAS. NET platform development guide-series Directories

Introduction to AgileEAS. NET application development platform-Index

AgileEAS. NET platform application development tutorial-case plan

Official website of AgileEAS. NET

Agile Software Engineering Lab

QQ: 116773358

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.