Operate the table class of a single table by using LINQ to SQL

Source: Internet
Author: User

The datacontext class is introduced earlier. It can be used to map and connect to databases, execute SQL commands, and track the status of object objects.

The following describes table <tentity>, which indicates table record. It is a generic collection class and its elements are table entity objects. It provides a set of methods to add and delete elements and save these operations to the database through datacontext.

The table is also the previous table, and a LINQ to SQL class is added to the project. The focus is on insertonsubmit, deleteonsubmit, and other methods.

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text;  Namespace  Linq_to_ SQL _table {  ///  <Summary>      ///  Operate a single table  <Tentity>  Class  ///   </Summary>      Class  Program {  Static   Void Main ( String  [] ARGs ){  //  1. A. attach an object Dataclasses1datacontext DC1 =New  Dataclasses1datacontext (); tb_guestinfo guset = New Tb_guestinfo () {id = 1 , Name = "  Debuglzq  " , Age = 35 , Tel = "  138 **** 8888  "  }; Dc1.tb _ guestinfo. Attach (guset );  //  Such attach only attaches entities, and the database is not updated. Dc1.submitchanges ();  //  Show attached successfully              Foreach ( VaR G In  Dc1.tb _ guestinfo) {console. writeline (  "  {0} {1} {2} {3}  "  , G. ID, G. Name, G. Age, G. Tel);} console. writeline (  "  ---------  " );  //  Show database not updated Dataclasses1datacontext DC2 = New  Dataclasses1datacontext ();  Foreach ( VaR G In  Dc2.tb _ guestinfo) {console. writeline (  "  {0} {1} {2} {3}  "  , G. ID, G. Name, G. Age, G. Tel);} console. writeline ( "  ------------------------  "  ); Console. readkey ();  //  2. insertonsubmit  Dc2.tb _ guestinfo. insertonsubmit (guset); dc2.submitchanges ();  Foreach ( VaR G In  Dc2.tb _ guestinfo) {console. writeline (  "  {0} {1} {2} {3} "  , G. ID, G. Name, G. Age, G. Tel);} console. writeline (  "  ------------------------  "  ); Console. readkey ();  //  2b. insertallonsubmit insert set List <tb_guestinfo> lst = New List <tb_guestinfo> (){  New Tb_guestinfo () {name = " AA  " , Age = 25 , Tel = "  133 **** 3333  "  },  New Tb_guestinfo () {name = "  Bb  " , Age = 25 , Tel = "  135 **** 5555  " }; Dc2.tb _ guestinfo. insertallonsubmit (LST); dc2.submitchanges ();  Foreach ( VaR G In  Dc2.tb _ guestinfo) {console. writeline (  "  {0} {1} {2} {3}  "  , G. ID, G. Name, G. Age, G. Tel);} console. writeline (  "  ------------------------  " ); Console. readkey ();  //              //  3. deleteonsubmit Tb_guestinfo entity = ( From G In  Dc2.tb _ guestinfo  Where G. Name = "  AA  "                                     Select  G). Single (); dc2.tb _ guestinfo. deleteonsubmit (entity ); //  Dc2.submitchanges ();  Foreach ( VaR G In  Dc2.tb _ guestinfo) {console. writeline (  "  {0} {1} {2} {3}  "  , G. ID, G. Name, G. Age, G. Tel);} console. writeline (  "  ------------------------  " ); Console. readkey ();  //  3B. deleteallonsubmit Ienumerable <tb_guestinfo> entitys = From G In  Dc2.tb _ guestinfo  Where G. Name = "  AA  " | G. Name = "  Bb  "                                                 Select  G; dc2.tb _ guestinfo. deleteallonsubmit (entitys); dc2.submitchanges ();  Foreach ( VaR G In  Dc2.tb _ guestinfo) {console. writeline (  "  {0} {1} {2} {3}  "  , G. ID, G. Name, G. Age, G. Tel);} console. writeline (  "  ------------------------  " ); Console. readkey ();}}} 

ProgramThe running result is as follows:

 

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.