The old Insert and retrieving your Serial problem in VB

From: John McCawley <nospam(at)hardgeus(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: The old Insert and retrieving your Serial problem in VB
Date: 2006-11-14 21:54:42
Message-ID: 455A3B22.8070505@hardgeus.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am once again dealing with that age old problem of retrieving the
value of your inserted serial field in VB. I am fully aware that I can
manually pull the currval or nextval from my sequence, but I consider
having to manually deal with an auto-generated database object an
*extremely* inelegant solution. In every language I have used with
PostgreSQL so far (VB, PHP, C++) I have attempted to create an
abstracted way of retrieving the generated serial. I have accomplished
this in PHP and C++ with the following solution:

I have an abstracted SQL Query class with a method that looks roughly
like this:

//Will return the value of the inserted record at $sColumnName
$obDB->RunInsert( $sSQL, $sTableName, $sColumnName );

Inside the method, after the insert succeeds, it calls a method like so:

function AutonumberWorkaround( $sTableName, $sColumnName ) {
//Need to do a workaround here...
if( $this->m_nAutonumber && ($this->m_sDriver ==
"postgres" || (!$this->m_sDriver && DATABASEDRIVER == "postgres" ) ) ) {
$conn = $this->GetConnection();
$rs = $conn->Execute("SELECT " . $sColumnName .
" FROM " . $sTableName . " WHERE OID = " . $this->m_nAutonumber);
if( $rs ) {
$this->m_nAutonumber =
$rs->fields[$sColumnName];
}
else {
echo "<FONT COLOR=RED>Error retrieving
autonumber for " . $sTableName . "(OID " . $this->m_nAutonumber . ")<BR>";
return(-1);
}
}

return( $this->m_nAutonumber );
} //AutonumberWorkaround

(m_nAutonumber is set in the RunInsert as the OID returned from the
statement)

I am trying to implement something (ANYTHING!) in VB that will allow me
to retrieve my serials **without** having to manually pass in the
sequence name. Unfortunately, I can't seem to figure out how to
retrieve the inserted OID when using ADO. I have tried using the ado
recordset object and use rs.Open on a ado command object, but the
resulting recordset is in the closed state and seems to have no data. I
have using the ado command execute method, but it doesn't seem to return
anything. Is there any permutation of ADO commands that will return the
OID of an inserted record?

I am even willing to use the crappy writeable recordset syntax:
rs.Insert
rs("foo") = bar
rs.Update

But, once again, my recordset's serial column is not being updated (And
bear in mind that I am using my ADO class which worked fine in MS SQL
Server, so I'm pretty sure that my cursor settings etc. are good...for
the record I'm using adOpenDynamic, adLockOptimistic for the recordset,
and adUseClient for the server)

John

Responses

Browse pgsql-general by date

  From Date Subject
Next Message MicroUser 2006-11-14 22:03:40 ORDER BY
Previous Message Scott Marlowe 2006-11-14 21:37:17 Re: LAN