Get the autonumber of the row inserted

P

Praveen

Hi,
I wanted to insert a record in to a database and then get the autonumber of
the row that is inserted using AdoDotnet.

this can be easily done in ADO like below.

TCHAR* szSQL = { _T( "select processid,identifier,emailid,statusmsg from
backgroundprocess where 1=2" ) };
/*open a recordset with the above sql*/
pRsProcessID->AddNew();
pRsProcessID->Fields->GetItem((long)1)->Value = lIdentifier;
pRsProcessID->Fields->GetItem((long)2)->Value = _tcslwr( *bsEmailID );
pRsProcessID->Fields->GetItem((long)3)->Value = szStatusMsg;
pRsProcessID->Update();
lProcessID = (long)pRsProcessID->Fields->GetItem((long)0)->Value;

What steps should I follow to achieve the same with AdoDotnet.

Thanks in advance
Praveen
 
C

Cor Ligthert [MVP]

Praveen,

Bill Vaughn has written a lot of this in this newsgroup, I saw him make a
warning yesterday. (correct because you get only the latest autonumber back
with @@identity and you are probably updating more if you use a datatable).

Have a look at his website, there should be how to handle this correct.

http://www.betav.com/

Cor
 
P

Praveen

Hello Kevin,
I was trying to port some of the functionalities in my companies product to
dotnet. our product supports both oracle and sql server. I did not want to
write code specific to oracle or sql database....
This kind of abstraction was seamlessly possible using ado and oledb
providers to different databases. could you please let me know if the same
kind of abstraction is possible with adodotnet. if yes then how to proceed.

Thanks in advance,
Praveen
 
K

Kevin Yu [MSFT]

Hi Praveen,

As far as I know, the method I provided in my last post works in SQL server
and Access database. However, since ADO.NET uses a different object model
from ADO, this cannot be working for all databases. If you need them for
all databases, you can try to interop ado in your .net app. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top