First, let's look at a simple insert operation:
Testdatacontext Dc = new testdatacontext ();
Protected void page_load (Object sender, eventargs E)
{
Tbl_user user = new tbl_user ();
User. uname = "XY ";
User. upwd = "111 ";
DC. tbl_user.insertonsubmit (User );
DC. submitchanges ();
Int id = user. uid;
}
Int id = user. UID to get the inserted value! Why?
1. When we set the uid in the database, we set it as the primary key, and then to indicate the column, auto-growth, the seed is 1, and the growth volume is 1.
2. Drag the data table to vs. We don't just need to drag the data table, but also read it.
[Global: system. Data. LINQ. Mapping. columnattribute (storage = "_ uid ",Autosync = autosync. oninsert,Dbtype= "Int not null identity ",Isprimarykey= True,Isdbgenerated= True)]
Public int uid
{
Get
{
Return this. _ uid;
}
Set
{
If (this. _ uid! = Value ))
{
This. onuidchanging (value );
This. sendpropertychanging ();
This. _ uid = value;
This. sendpropertychanged ("uid ");
This. onuidchanged ();
}
}
}
Autosync = autosync. oninsert indicates automatic synchronization in plug-in,
Isdbgenerated indicates the value automatically generated by the database.
This is amazing! Understand.
You can also set these attributes through a visual view.
For more information about how to obtain this ID using a pure SQL statement, see this blog article:
How to obtain the self-increasing id value just inserted in SQL Server
Http://blog.sina.com.cn/s/blog_67aaf4440100w7ns.html