Pdf. NET data development framework for MySQL object operation instances

Source: Internet
Author: User

In our recent project, SQL-MAP is used more, but the entity class is rarely used, in fact, the entity class of "pdf. NET data development framework" is quite powerful, the following testProgramIs an instance operated in MySQL.

1. First, configure the database connection string in the app. config file:

 <?  XML   Version  =  "  1.0  "   Encoding  =  "  UTF-8  "   ?>  <  Configuration >    <  Connectionstrings  >      <  Add   Name   =  "  Default  "   Connectionstring   =  "  Server = 192.168.xx.xx; user id = root; Password = xxxx; database = test  "   Providername  = "  Pwmis. dataprovider. Data. MySQL, pwmis. mysqlclient  "  />    </  Connectionstrings  >  </  Configuration  > 

2. Then define a "user" entity class:

 /** Pdf. NET data development framework * http://www.pwmis.com/sqlmap */  Using System; Using System. Collections. Generic; Using System. LINQ;Using System. text; Using Pwmis. datamap. entity; Namespace Testmysqlentity { Class User: entitybase { Public User () {tablename = "Tb_user" ; Primarykeys. Add ( "ID" ); // Primary key Identityname = "ID" ; // ID, auto-Increment Propertynames = New  String [] { "ID" , "Name" , "Age" }; Propertyvalues = New   Object [Propertynames. Length];} Public   Int Id { Get { Return Getproperty < Int > ( "ID" );} Set {Setproperty ( "ID" , Value );}} Public   Int Age { Get { Return Getproperty < Int > ( "Age" );} Set {Setproperty ( "Age" , Value );}} Public   String Name { Get {Return Getproperty < String > ( "Name" );} Set {Setproperty ( "Name" , Value, 50 );}}}}

3. Based on this object class, we define a user table tb_user in MySQL. The specific process is omitted.

(This goal is to first have an entity and then a data table, so that you can understand the real meaning of the Orm)

 

4. Write An ORM object Operation TestCode:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Pwmis. datamap. entity;

NamespaceTestmysqlentity
{
ClassProgram
{
Static VoidMain (String[] ARGs)
{
User u= NewUser ();

// ************** Construct an oql query expression ************
// Query object sets
// Use the oqlcompare object as a condition
// Oql q = oql. From (U). Select (). Where (New oqlcompare (U). comparer (U. Age, oqlcompare. comparetype. nosmaller, 15). end;

Oql Q =   New Oql (U );
// Use oql2 as the condition object
Q. Select (). Where (Q. condition. And (U. Age, " > = " , 15 ). Orderby (U. Age, " ASC " );
// The queryparameter array is used as the condition and is suitable for multiple parallel and conditions.
// Q. select (). where (New queryparameter [] {New queryparameter ("Age", pwmis. common. enumcompare. nosmaller, 15 )}). orderby (U. age, "ASC ");
Console. writeline ( " Oql to SQL: \ r \ n " + Q. tostring ());


//************

// query the Object List
var result = entityquery User > . querylist (Q);
console. writeline ( " the object set is queried successfully. Quantity: " + result. count);

Console. writeline ("\ R \ nexecuted SQL text: \ r \ n {0} \ r \ n", Pwmis. dataprovider. Data. commandlog. instance. commandtext );

// Query a single object
U. Name =   " Zhang San " ;
Q. Select (). Where (U. Name );
Console. writeline ( " Oql to SQL: \ r \ n "   + Q. tostring ());
User u1 = Entityquery < User > . Queryobject (Q );
If (U1 ! =   Null )
Console. writeline ( " A single object is queried! " );

Console. writeline ("\ R \ nexecuted SQL text: \ r \ n {0} \ r \ n", Pwmis. dataprovider. Data. commandlog. instance. commandtext );

// Directly use entityquery <t>. instance Attribute Insertion, modification, and deletion Methods
U. Name =   " Li Si3 " ;
U. Age =   15 ;
If (Entityquery < User > . Instance. insert (u) >   0 )
Console. writeline ( " Object inserted! " ); // The ID attribute is automatically assigned a value.

Console. writeline ( " \ R \ nexecuted SQL text: \ r \ n {0} \ r \ n " , Pwmis. dataprovider. Data. commandlog. instance. commandtext );

U. Age =   25 ;
If (Entityquery < User > . Instance. Update (u) >   0 )
Console. writeline ( " Object modified! " );

Console. writeline ("\ R \ nexecuted SQL text: \ r \ n {0} \ r \ n", Pwmis. dataprovider. Data. commandlog. instance. commandtext );

User U2= NewUser ();
U2.name= "Wang Wu";
U2.age= 20;

// Update an object using the Instance Object method of entityquery <t>
// Only the assigned attribute values are updated.
Entityquery < User > EQ =   New Entityquery < User > (U2 );
If (Eq. saveallchanges () >   0 )
Console. writeline ( " Entity updated! " );

Console. writeline ("\ R \ nexecuted SQL text: \ r \ n {0} \ r \ n", Pwmis. dataprovider. Data. commandlog. instance. commandtext );

Console. Read ();
}
}
}

 

5. Compile and run the program to get the following results:

Oql to SQL:
Select [ID], [name], [age]
From [tb_user]
Where [age]> = @ age0
Order by [age] ASC
Object set queried successfully. Quantity: 23

Executed SQL text:
Select 'id', 'name', 'age'
From 'tb _ user'
Where 'age'> = @ age0
Order by 'age' ASC

Oql to SQL:
Select [ID], [name], [age]
From [tb_user]
Where name = @ name

A single object is queried!

Executed SQL text:
Select 'id', 'name', 'age'
From 'tb _ user'
Where name = @ name

Object inserted!

Executed SQL text:
Insert into 'tb _ user' ('name', 'age') values (@ P0, @ P1)

Object modified!

Executed SQL text:
Update 'tb _ user' set 'age' = @ P0 where 'id' = @ p1

Entity updated!

Executed SQL text:
Insert into 'tb _ user' ('name', 'age') values (@ P0, @ P1)

 

6. Result description

We can see that the entire operation is successful. Pay special attention to this:

 
Update'Tb _ user'Set'Age' = @ P0Where'Id' = @ p1

At that time, we only assigned a new value to the age attribute, so the generated update statement only updated this field.

The object-type query oql expression can be constructed using multiple where conditions. For details, see the above Code.

 

Related Article

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.