Hi all,
Can any one help me how to get a field (auto-number field)
after I add a new row to the table using ExecuteNonQuery() method
- string insertStmnt = @"INSERT INTO Student (stName) VALUES (@stName)";
-
OleDbCommand comm = new OleDbCommand(insertStmnt, myconn);
-
OleDbParameter param = new OleDbParameter("@stName",stNameVal);
-
comm.Parameters.Add(param);
-
int rowNum = comm.ExecuteNonQuery();
so, how do we select the rowNum row from a table?
I'm using MS Access
plz help me...
thanks in advanced ....
in any time you can use a second sql-query....if your auto number are ascending then you can easily retrieve it with
SELECT MAX(myAutonum_id) FROM mytable
but like sql-server Access also support
@@IDENTITY read this.....................I have found it in another site....I have no link for that site......but i have the functions saved
We have a table in MsAccess like :
Test, Fields (id=autoinc, name=text);
First we have to have a function like the one below :
function GetLastInsertID : integer;
begin
// datResult = TADODataSet
datResult.Active := False;
datResult.CommandText := 'select @@IDENTITY as [ID]';
datResult.Active := True;
Result := datResult.FieldByName('id').AsInteger;
datResult.Active := False;
end;
Now before getting the last inserted record record id = autoincrement field, in other words calling the above function. You have to do a SQL insert like the following
procedure InsertRec;
begin
// datCommand = TADOCommand
datCommand.CommandText := 'insert into [test] ( [name] ) values ( "Test" )';
datCommand.Execute;
end;
Now if we like to know which is the last autoinc value ( notice that the getlastinsertid proc. only works after the insertrec proc)
procedure Test;
begin
InsertRec;
Showmessage(format('lastinsertid : %d',[GetLastInsertID]));
end;