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