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: