The previous four articles demonstrate some conventional operations and configurations of the ORM. Through the previous articles, application developers should be able to use the ORM to develop simple applications. However, the ORM is not the omnipotent key, in business applications, many problems cannot be solved by ORM. For these problems, we must use SQL statements.
Focus of this Article
Since ORM cannot solve all the problems, we have to use SQL statements, that is, we can understand that in the DAL layer, in the orm object, we have to support SQL operations:
The orm on the AgileEAS. NET platform provides this support. It queries and executes SQL statements through the DataAccessor attribute PROVIDED BY THE ORM object, that is, the programming method SUPPORTED BY THE ORM Component:
This. DataAccessor. Execute (SQL statement, parameter ).
For more information about how to use UDA objects, see step-by-step instructions for using AgileEAS. NET basic library for application development-basics-use UDA to manipulate SQL statements, step by step teach you to use AgileEAS.. NET basic library for application development-basics-delegation and Application in UDA, step by step teach you to use AgileEAS.. NET basic library for application development-basics-processing transactions in UDA.
Demo
This article demonstrates how to obtain the largest IDN number in the commodity Dictionary Based on the previous demonstration. First, we modify ClassLibDemo. DAL. the Product of the SQL server project. cs file, add the following code to the class Product:
1 /// <summary>
2 /// the maximum size of the record.
3 /// </summary>
4 /// <returns> </returns>
5 public int GetMaxIdn ()
6 {
7 return (int) this. DataAccessor. Query ("select isnull (max (idn), 0) maxid from" + this. DbTableName );
8}
9
10 /// <summary>
11 /// the maximum size of the record.
12 /// </summary>
13 /// <returns> </returns>
14 public int GetMaxNewIdn ()
15 {
16 return (int) this. DataAccessor. Query ("select isnull (max (idn), 0) + 1 maxid from" + this. DbTableName );
17}
Then we add the following code in the DataObjectDemo of the ClassLib. OrmDemo project:
1 /// <summary>
2 /// Add a record to the demo.
3 /// </summary>
4 public void DemoInsert2 ()
5 {
6 Product product = new Product ();
7 product. OrmAccessor = OrmContext. OrmAccessor;
8
9 product. Idn = product. GetMaxNewIdn ();
10 product. Code = "AXX-" + product. Idn. ToString ("D6 ");
11 product. Name = "desktop computer ";
12 product. Spec = "mainstream configuration/clip meat i7/21 display ";
13 product. Unit = "platform/set ";
14 product. Description = string. Empty;
15
16 try
17 {
18 product. Insert ();
19 System. Console. WriteLine ("successfully inserted records ");
20}
21 catch (System. Exception exc)
22 {
23 System. Console. WriteLine ("insert record error:" + exc. Message );
24}
25}
26
27 /// <summary>
28 /// demonstrate reading a record.
29 /// </summary>
30 public void DemoQuery2 ()
31 {
32 Product product = new Product ();
33 product. OrmAccessor = OrmContext. OrmAccessor;
34 product. Code = "AXX-" + product. GetMaxIdn (). ToString ("D6 ");
35 product. Refresh (); // read data
36
37 if (product. Exists)
38 {
39 System. console. writeLine (string. format ("queried to record: idn = {0} \ tCode = {1} \ tName = {2} \ tSpec = {3} \ tUnit = {4} \ tDescription = {5} ", product. idn, product. code, product. name, product. spec, product. unit, product. description ));
40}
41 else
42 {
43 System. Console. WriteLine ("this record does not exist ");
44}
45}
Add the following code to the process control code:
System. console. writeLine ("press any key to continue... "); System. console. read (); System. console. writeLine ("example of demonstrating a user-defined business (taking the largest Insert):"); new DataObjectDemo (). demoInsert2 (); new DataObjectDemo (). demoQuery2 ();
Compile and run, and output the result:
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