Open source object relationship ing tool Orm. Net insert data insert/update data

Source: Internet
Author: User
Tags dsn
Datamanager. New [object] Create data records

The followingCodeCreate a student record and save it to the database.

 
Datamanager dm =NewDatamanager (config. DSN );// Assign a new object referenceStudent s = DM. newstudent (); S. firstname = "Mike"; S. lastname = "Smith"; DM. commitall ();

 

For data tables with master-slave relationships, student and contact, Orm. NET will help process the foreign key reference relationships between them.

Datamanager dm =NewDatamanager (config. DSN); student s = DM. newstudent (); S. firstname ="Tim"; S. lastname ="Brown"; S. Contact = DM. newcontact (); S. Contact. address1 =& Quot; 555 Main Street & quot"; S. Contact. address2 ="Apt 6"; S. Contact. City ="Oakland"; S. Contact. State ="Ca"; S. Contact. postalcode ="80304"; S. Contact. Country ="Us";// Create both records and maintain the relationship between them in DatabaseDM. commitall ();
 

 

Add the sub-object add [childobject] Methods

The add [child] method allows you to add new sub-objects. During commitall, Orm. Net automatically maintains their primary/foreign key relationships.

Datamanager dm =NewDatamanager (config. DSN );// Create new student objectStudent = DM. newstudent (); student. firstname ="Cherry"; Student. lastname ="Jackson";// Create a new contactContact contact = DM. newcontact (); contact. address1 ="55 6th Avenue";// Add other contact information ..// Add the new student to the new contact objectContact. addstudent (student); DM. commitall ();
 
 
New [childobject] Methods
// Find an existing contact recordDM. querycriteria. And (joinpath. Contact. Columns. address1,& Quot; 123 Main Street & quot"); Contact = DM. getcontact (fetchpath. All); Student = contact. newstudent ();// Create a new child object-studentStudent. firstname ="Tom"; Student. lastname ="Smith";// Commitall () will create a new student object and relate it to the contact objectDM. commitall ();

If the object is already in memory, you can create a new sub-object in newchild mode and save it to the database when you call commitall.

 

UsingRequireSetting on new [object] Methods for column Properties

You can set the attribute firstname to required in the orm. Net Object Browser, which must be input. At the same time, the newstudent method signature will also change to pass in the firstname value.

The code for the new object in Singapore is as follows:

// Firstname is now required to create new student objectStudent s = DM. newstudent ("Mike");// Add a lastname value as well if you like...S. lastname = Smith; DM. commitall ();

The first parameter, Mike, is firstname. Generally, you only need to set the primary key field column to required.

 

UsingRequireParent object new [object] Methods on child records

The required attribute can also be used during the creation of sub-objects, as shown in

After the settings shown, a method signature in newobject is required for cocntact.

Datamanager dm =NewDatamanager (config. DSN );// Create a new contact object with some informationContact contact = DM. newcontact (); contact. address1 ="123 require parent"; Contact. City ="Boulder"; Contact. Country ="Us";// Contact object is now requiredStudent = DM. newstudent (contact); student. firstname ="John"; Student. lastname ="Glass";// Create the new student and related contact recordDM. commitall ();

Attribute and its parent object can be set to required at the same time, as in the following code:

 
// A firstname property and contact object are now required to create a new student objectContact = DM. getcontact (); Student = DM. newstudent ("Tom", contact );
 
 
Update Data
Datamanager dm =NewDatamanager (config. DSN); DM. querycriteria. Clear (); DM. querycriteria. And (joinpath. Teacher. Columns. firstname,"Tom", Matchtype. Exact); Teacher = DM. getteacher (fetchpath. Teacher); teacher. firstname ="Thomas"; Teacher. lastname ="Franklin"; DM. commitall ();// Will perform required update operation as a transaction

The commitall method generates the following SQL statement.

Begin TransactionUpdate[Teacher]Set[Lastname] ='Franklin', [Firstname] ='Thomas'WhereId = 10008If @ Error<> 0BeginRollback TransactionReturnEndCommit Transaction


Datamanager. commitalldebug

For debuggingProgramYou can use commitalldebug to generate all SQL statements, but do not submit the SQL statements to the database for execution.

 DM. querycriteria. clear (); DM. querycriteria. and (joinpath. teacher. columns. firstname,  "Tom" , matchtype. exact); Teacher = DM. getteacher (fetchpath. teacher); teacher. firstname =  "Thomas" ; teacher. lastname =  "Franklin" ;  // will not perform any changes but will generate the SQL statement (s) that wocould be   // used in the output window  DM. commitalldebug (); 

In this case, you can view the generated SQL statement in the output window to check whether the logic is correct.

If datamanager. commitall is called, you can obtain the SQL statement of the last transaction by calling its attribute lastcommittext.

 datamanager dm =  New  datamanager (config. DSN); DM. querycriteria. clear (); DM. querycriteria. and (joinpath. teacher. columns. firstname,  "Tom" , matchtype. exact); Teacher = DM. getteacher (fetchpath. teacher); teacher. firstname =  "Thomas" ; teacher. lastname =  "Franklin" ; DM. commitall ();  // ouput generated SQL statement to console  console. writeline ( "output SQL"  + DM. lastcommittext); 

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.