Table<tentity> represents a tabular record, which is a generic collection class whose elements are table entity objects. It provides a set of methods for adding and deleting elements, and you can save them to the database through DataContext.
Table or the previous table, add a LINQ to SQL class to the project. The focus is insertonsubmit, DeleteOnSubmit and other methods.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Linq_to_sql_table
{
<summary>
Table<tentity> class for manipulating a single table
</summary>
Class Program
{
static void Main (string[] args)
{
1.a.attach Additional Entities
Dataclasses1datacontext DC1 = new Dataclasses1datacontext ();
Tb_guestinfo guset = new Tb_guestinfo () {id=1, Name = "Debuglzq", age = +, Tel = "138****8888"};
Dc1.tb_GuestInfo.Attach (Guset);//Such Attach only attach entities, the database is not updated
DC1. SubmitChanges ();
Show additional success
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 is 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 inserting a collection
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 ();
}
}
}
The results of the program run as follows:
c#→ about the table<tentity> generic class under System.Data.Linq