5. CRUD operation:
Crud (that is, the CREATE, retrieve, update, and delete methods) is the basic operation of the database.ProgramPersonnel may have been repeat these simple post-job jobs. The biggest contribution of ORM is to free programmers from this state and focus on the design of Business Entity objects.
(1) Create, update, and delete object classes
Snake. net uses objectmanager objects to achieve persistent work with databases. objectmanager uses the SAVE and delete methods to create, update, and delete Business Entity objects. See the followingCode:
// Declare
Customer customer;
Customer = New Customer ( " Zzcfr " );
Customer. companyName = " Ana Trujillo emparedados y helados " ;
Customer. contactname = " Antonio Moreno " ;
Customer. contacttitle = " Owner " ;
Customer. Address = " Forsterstr. 57 " ;
Customer. City = " Berlin " ;
Customer. postalcode = " T2f 8m4 " ;
Customer. Country = " Canada " ;
Customer. Phone = " (604) 555-4729 " ;
Using (Iobjectmanager om
= Objectmanager. Create ( Typeof (Customer ))) {
Om. Save (customer );
}
Using (Iobjectmanager om
= Objectmanager. Create ( Typeof (Customer ))) {
Customer=Om. Retrieve (1)AsCustomer;
Om. Delete (customer );
}
(2) Using Stored Procedures for creation, update, and deletion
By default, Snake. Net automatically generates and operates SQL statements based on the ing between the business entity object and the data table structure. However, you can create, update, and delete a configuration file using the stored procedure as needed. Refer to the following configuration nodes. You can set insertprocedure, updateprocedure, and deleteprocedure to run the specified Stored Procedure operations. < Object Name = "Eastasp. Enterprise. Security. Authorization. Session" Groupname = "Enterprise. Main" >
< Insertprocedure Name = "Usp_sessioninsert" Params = "-All -" />
</ Object >
Figure 5-2-1 configure the stored procedure name
(3) Transaction Processing
Snake. Net can be used to process transactions. One is to use objectmanager's Transaction Processing Methods: begintransaction, commit, and abort. See the following code:
// Declare
Category C1, C2;
C1 = New Category ();
C1.categoryname = " Seafood1 " ;
C1.description = " Seaweed and fish " ;
C2 = New Category ();
C2.categoryname = " Seafood2 " ;
C2.description = " Seaweed and fish " ;
Using (Iobjectmanager om
= Objectmanager. Create ( Typeof (Category ))) {
Try
{
Om. begintransaction ();
Om. Save (C1 );
Om. Save (C2 );
Om. Commit ();
}
Catch (Exception ex)
{
Om. Abort ();
ThrowEx;
}
}
Another method is to useTransactioncontextClass, call the transaction environment for transaction processing, see the following code:
// Declare
Shipper shipper;
Customer customer;
Iobjectmanager om1, om2;
Shipper = New Shipper ();
Shipper. companyName = " Speedy Express " ;
Shipper. Phone = " (503) 555-9831 " ;
Customer = New Customer ( " Zdefr " );
Customer. companyName = " B 'Lido comidas preparadas " ;
Customer. contactname = " Na Trujillo " ;
Customer. contacttitle = " Owner " ;
Customer. Address = " Forsterstr. 57 " ;
Customer. City = " Berlin " ;
Customer. postalcode = " T2f 8m4 " ;
Customer. Country = " Canada " ;
Customer. Phone = " (604) 555-4729 " ;
Using (Transactioncontext TC =
Transactioncontext. Create ()) {
Om1=Objectmanager. Create (Typeof(Shipper ));
Om2=Objectmanager. Create (Typeof(Customer ));
Om1.save (shipper );
Om2.save (customer );
TC. consistent= True;
(4) Accept Business Entities
Snake. Net can be used to Accept Business Entity objects through the retrieve and list methods of objectmanager. The former can accept individual business entities based on keywords, and the latter can accept a group of business entities based on a set of keywords. See the following code: Customer customer;
Using (Iobjectmanager om
= Objectmanager. Create ( Typeof (Customer ))) {
Customer=Om. Retrieve (1)AsCustomer;
}