Retrieve Identity Column

W

WhiteEagl

Hello,

I would need some help with this identity column problem in
SQLServer.

I have a database with two tables. The Parent table has an Identity
column.

Parent (ParentID IDENTITY, Name)
Child (ChildID, Name, ParentID)

In C#, when I try to create a Parent and Child info, I have a problem
with the identity column. How can I create a Parent row with many
child rows and then update the DataBase? Can I retreive the identity
column from the parent row?

I am also using a .xsd (DataModel) to make things easier when using
DataSets.
 
M

Miha Markic

Hi,

WhiteEagl said:
Hello,

I would need some help with this identity column problem in
SQLServer.

I have a database with two tables. The Parent table has an Identity
column.

Parent (ParentID IDENTITY, Name)
Child (ChildID, Name, ParentID)

In C#, when I try to create a Parent and Child info, I have a problem
with the identity column. How can I create a Parent row with many
child rows and then update the DataBase? Can I retreive the identity
column from the parent row?

In dataset you set identity column AutoIncrement = true, AutoIncrementSeed
= -1 and AutoIncrementStep = -1
So it creates negative keys.
InsertCommand should have at least identitity column retrieve statament at
the end, something like:
INSERT INTO .... ; SELECT SCOPE_IDENTITY
So the datatable gets real identity value after database update.
Check out
HOW TO: Retrieve an Identity Value from a Newly Inserted Record from SQL
Server by Using Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;320897&Product=NETFrame
Note - it has an error - it uses @@identity instead of scope_identity.
 

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