Returns the latest ID method when inserting SQL data.

Source: Internet
Author: User
Tags mysql tutorial

Returns the latest ID method when inserting SQL data.

SELECT LAST_INSERT_ID (). SELECT @ IDENTITY can be connected. But that brother hasn't done it by the afternoon. Because I don't know how to run it .. Projects are all three layers. It is basically written in DBHELPER. The reason is that it is written:

Cmd. ExecuteNonQuery (); --- this is the number of affected rows returned by executing a non-query statement.
In fact, it should be written:

Cmd. ExecuteScalar ();
Returns the first column of the first row. It may be because the comment in helper is:

 

/// <Summary>
/// Execute a query result statement and return the query result (object ).
/// </Summary>
/// <Param name = "SQLString"> calculate the Query Result Statement </param>
/// <Returns> query result (object) </returns>


LAST_INSERT_ID () mysql tutorial _ insert_id: Get the automatic number of the primary key value of the insert data. So let's analyze the differences between them.
Mysql_insert_id () is to directly obtain the insert_id of the current session, while LAST_INSERT_ID () is an SQL function, which needs to be obtained by executing an SQL statement, such as: SELECT LAST_INSERT_ID (); obviously, these two results are the same, but it takes a lot of resources to execute an SQL statement. It is much faster to directly obtain the memory value through mysql_insert_id ().

Related Article

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.