/// <summary> ///get self-increment variable value/// </summary> /// <returns>self-increment variable value</returns> Public intgetreturnidentity () {//Conn_open (); stringstrCmdText =@"SELECT @ @identity"; OleDbCommand Cmd_sql=NewOleDbCommand (strCmdText, conn_1); inti =int. Parse (Cmd_sql. ExecuteScalar (). ToString ()); Cmd_sql. Dispose (); returni; //conn_close ();}
Get automatic numbering of newly added rows in Access (go from http://www.cnblogs.com/hongyuniu/archive/2008/03/08/1096638.html) in SQL Server there is a global variable @ @IDENTITY, he used to record the current link generated by the Auto plus 1 value, this variable can also be used in access, and now we use this @ @Identity in the transaction to get the new added row of the automatic numbering. Note that the command to get the AutoNumber and insert the record must be executed at the same time that the database connection is opened or the SELECT @ @Identity returns 0. The code is as follows:
String SCon;
SCon = system.configuration.configurationmanager.connectionstrings["Access". ConnectionString;
con = new OleDbConnection (SCon);
Con. Open ();
int i =-1;
OleDbCommand cmd = con. CreateCommand ();
oledbtransaction tr = con. BeginTransaction ();
Cmd. Transaction = TR;
Cmd.commandtext = ...;..;
Try
{
i = cmd. ExecuteNonQuery ();
if (i > 0)
{
Cmd.commandtext = @ "SELECT @ @identity";
i = Int. Parse (cmd. ExecuteScalar (). ToString ());
}
Tr.commit ();
}
catch (Exception e)
{
MessageBox.Show (E.message);
}
This. Close ();
return i;