How do you get the @@identity for the record just inserted?
Posted by: Timothy Graupmann
Date: January 23, 2005 12:54PM

How do you get the @@identity for the record just inserted?

Example code: (Not working yet)

static public UInt32 AddVertex3d(Vertex3d newVertex3d)
{
UInt32 Vertex3dId = 0;

IDbConnection myConnection = DBFactory.CreateConnection();
myConnection.Open();
try
{
IDbCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "INSERT INTO tagml_vertex3d SET MeshNodeId=?id,X=?x,Y=?y,Z=?z,LastModified=sysdate()";
IDbDataParameter p = myCommand.CreateParameter();
p.ParameterName = "?id";
p.Value = newVertex3d.MeshNodeId;
AddDbTypeToParameter(myCommand, p);
p = myCommand.CreateParameter();
p.ParameterName = "?x";
p.Value = newVertex3d.X;
AddDbTypeToParameter(myCommand, p);
p = myCommand.CreateParameter();
p.ParameterName = "?y";
p.Value = newVertex3d.Y;
AddDbTypeToParameter(myCommand, p);
p = myCommand.CreateParameter();
p.ParameterName = "?z";
p.Value = newVertex3d.Z;
AddDbTypeToParameter(myCommand, p);

p = myCommand.CreateParameter();
p.ParameterName = "?Identity";
p.DbType = DbType.UInt32;
p.Direction = ParameterDirection.Output;
AddDbTypeToParameter(myCommand, p);

myCommand.ExecuteNonQuery();
Vertex3dId = (UInt32)myCommand.Parameters["?Identity"];
}
catch(Exception e)
{
myConnection.Close();
throw e;
}
myConnection.Close();
return Vertex3dId;
}

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.