For database operations, the Id is obtained at the same time as the Insert operation to ensure atomic operation. Instead, take the Id first, and then insert it to the database.
Oracle:
Use the Returning into statement that comes with Oracle. The code example is as follows:
1 SQL = @ "INSERT INTO KEYWORD (KEYWORD_ID, SITE_ID, KEYWORD_NAME, STATUS, SOURCE, CREATE_TIME, LAST_CHANGED) 2 VALUES 3 (4 KEYWORD_SEQ.nextVal, 5: SITE_ID, 6: NAME, 7 1, 8: Source, 9 SYSDATE, 10 SYSDATE + 0.000711) RETURNING KEYWORD_ID into: Key_ID "; 12 13 com. commandText = SQL; 14 // com. parameters. add (new OracleParameter (": ID", OracleDbType. int64, allKeyWords. select (n => n. keyword_Id ). toArray (), ParameterDirection. input); 15 com. parameters. add (new OracleParameter (": SITE_ID", OracleDbType. int64, allKeyWords. select (n => n. site_Id ). toArray (), ParameterDirection. input); 16 com. parameters. add (new OracleParameter (": NAME", OracleDbType. NVarchar2, allKeyWords. select (n => n. keyword_Name ). toArray (), ParameterDirection. input); 17 com. parameters. add (new OracleParameter (": Source", OracleDbType. NVarchar2, allKeyWords. select (n => n. source ). toArray (), ParameterDirection. input); 18 var outputIdParm = new OracleParameter (": Key_ID", OracleDbType. int32, ParameterDirection. output); 19 com. parameters. add (outputIdParm); 20 21 com. executeNonQuery (); 22 23 var allKeyWordIds = outputIdParm. value as OracleDecimal [];View Code
Note the Returning statement in the above Code and the outPut of the final parameter.
SqlServer: Use the @ IDENTITY keyword that comes with SqlServer. The code example is as follows:
1 var cmd = dbMenloFramework. createCommand ("insert into syslog (Thread) values ('test') SELECT @ identity as Id"); 2 using (var read = dbMenloFramework. executeDataReader (cmd) {3 read. read (); 4 Console. writeLine (read [0]. toString (); 5}View Code
MySql:
It is temporarily missing. If some netizens have reliable methods, please contact me in the comments.