Retrieve Identity Column

  • Thread starter Thread starter WhiteEagl
  • Start date Start date
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.
 
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.
 
Back
Top