Return the auto-increment ID in the insert operation of LINQ to SQL.

Source: Internet
Author: User

 

       

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

 

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.