csharp:using OLE DB Getting the identity of the most recently added record

Source: Internet
Author: User
Tags ole

<summary>////Execute SQL statement, return the number of records affected///</summary>//<param name= "SQLString" >sql statement </param>///<returns> number of records affected </returns> public static int ExecuteSQL (string SQLString, para Ms Oledbparameter[] cmdparms) {using (OleDbConnection connection = new OleDbConnection (connectionString                    ) {using (OleDbCommand cmd = new OleDbCommand ()) {try                        {PrepareCommand (cmd, connection, NULL, SQLString, cmdparms); int rows = cmd.                        ExecuteNonQuery (); Cmd.                        Parameters.clear ();                    return rows; } catch (System.Data.OleDb.OleDbException E) {throw new Exce                    Ption (E.message);        }}}}///<summary>///Add a return ID valueTu 2014-12-29//Geovin Du///Http://www.mikesdotnetting.com/article/54/getting-the-identity-o F-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 name= "Cmdparms" ></param> <returns></returns> public static int ExecuteSQL (string SQLString, out int identity, params O Ledbparameter[] cmdparms) {using (OleDbConnection connection = new OleDbConnection (connectio nstring) {using (OleDbCommand cmd = new OleDbCommand ()) {TR                        y {preparecommand (cmd, connection, NULL, SQLString, cmdparms); 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 Exce                    Ption (E.message); }                }            }        }

  

csharp:using OLE DB Getting the identity of the most recently added record

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.