Returns the latest ID method when SQL inserts data

Source: Internet
Author: User
Tags mysql tutorial

Returns the latest ID method when SQL inserts data

SELECT last_insert_id (). SELECT @ @IDENTITY to receive. But by the afternoon the brother was not done. Because I don't know how to run. The projects are all three floors. The basic is written in the DBHelper. The reason is that it was written to:

Cmd. ExecuteNonQuery (); ---This is the number of rows that are returned affected by the execution of a non-query statement.
is actually supposed to write:

Cmd. ExecuteScalar ();
Returns the first header column. Maybe it's because the helper notes:

<summary>
Executes a calculated query result statement that returns the query result (object).
</summary>
<param name= "SqlString" > Compute query Result statement </param>
<returns> query Results (object) </returns>


last_insert_id () MySQL tutorial _insert_id get the primary key value AutoNumber of the INSERT data so analyze the difference.
MYSQL_INSERT_ID () is the insert_id that gets the current session directly, and last_insert_id () is a SQL function that needs to be obtained by executing SQL, such as: SELECT last_insert_id (); Obviously the two effects are the same, but it takes a lot of resources to execute the SQL once, and it's much faster to get the value in memory directly via MYSQL_INSERT_ID ()

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.