Csharp: using OleDb Getting the identity of the most recently added record, csharpadded

Source: Internet
Author: User

Csharp: using OleDb Getting the identity of the most recently added record, csharpadded

/// <Summary> /// execute the SQL statement, returned Number of affected records /// </summary> /// <param name = "SQLString"> Number of affected records </param> /// <returns> /returns> public static int ExecuteSql (string SQLString, params OleDbParameter [] partition parms) {using (OleDbConnection connection = new OleDbConnection (connectionString) {using (OleDbCommand cmd = new OleDbCommand () {try {PrepareCommand (cmd, connection, null, SQLString, writable parms); int rows = cmd. executeNonQuery (); cmd. parameters. clear (); return rows;} catch (System. data. oleDb. oleDbException E) {throw new Exception (E. message) ;}}}/// <summary> /// Add the returned consumer ID value // tu juwen // Geovin Du /// your exam: http://www.mikesdotnetting.com/article/54/getting-the-identity-of-the-most-recently-added-record /// http://stackoverflow.com/questions/186544/identity-after-insert-statement-always-returns-0 /// </summary> /// <param name = "SQLString"> </param> /// <param name = "identity"> </param/ param> /// <param name = "paiparms"> </param> /// <returns> </returns> public static int ExecuteSql (string SQLString, out int identity, params OleDbParameter [] partition parms) {using (OleDbConnection connection = new OleDbConnection (connectionString) {using (OleDbCommand cmd = new OleDbCommand () {try {PrepareCommand (cmd, connection, null, SQLString, partition parms); int rows = cmd. executeNonQuery (); cmd. commandText = "Select @ Identity"; identity = (int) cmd. executeScalar (); cmd. parameters. clear (); return rows;} catch (System. data. oleDb. oleDbException E) {throw new Exception (E. message );}}}}

  

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.