Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How about Oracle?

 

In Oracle I am using the following to accomplish this but I feel like it is not the correct way to get it done.

 

Dim strTranInsert As String = "INSERT INTO " & TransactionTable & " (TRAN_ID," & _
       " DG, TRAN_STARTTIME, DG_PATH) VALUES (TRAN_SEQ.NEXTVAL, '" & DGName & "', to_date('" & strStartTime & "','DD-MON-YYYY HH24:MI:SS'), '" & DGPath & "')"
       Dim strFetchID As String = "SELECT TRAN_SEQ.CURRVAL from " & TransactionTable
       Dim objTranInsertCmd As New OracleCommand(strTranInsert, OracleConn)
       Dim FetchID As OracleNumber 'The Sequence number assigned by Oracle

       Try
           OracleConn.Open()
           objTranInsertCmd.ExecuteNonQuery()
           objTranInsertCmd.CommandText = strFetchID    'Fetch back the unique id
           FetchID = DirectCast(objTranInsertCmd.ExecuteOracleScalar, OracleNumber)
       Catch ex As Exception
           Me.txtError.Text = "Error fetching id from transaction table: " & ex.Message
           blSuccess = False
       Finally
           OracleConn.Close()
       End Try

 

The reason I think this is the wrong way to go about it is because there is a time lag (albeit a short one) between the time the insert occurs and the id value is fetched. I should be alright because I am in a single user environment but this could be an issue if many records are being dumped in by many users.

 

For those unfamiliar, a sequence is Oracle's equivalent to the identity or autonumber field in MS.

Wanna-Be C# Superstar
Posted

Derek:

I've run into something with your version. I left the full SQL string off (because it is long and boring), but the rest looks like this:

Dim SQL as String = "INSERT INTO Table1 (...) VALUES (...)"
Dim Reader1 as OleDb.OleDbDataReader
Command1 = New OleDbCommand
Command1.Connection = New OleDbConnection(strConnection)
Command1.Connection.Open()
Command1.CommandText = SQL
Command1.ExecuteNonQuery()
Command1.CommandText = "SELECT @@IDENTITY" ' get ID of record
Reader1 = Command1.ExecuteReader
Command1.Connection.Close()

There is no Exception thrown, but on my Reader1 properties, I have:

 

Reader1

Depth - 0

FieldCount - 1

HasRows - True

IsClosed - False

Item - <cannot view indexed property>

RecordsAffected - 0

 

What do I need to read in? How do I get my ID number off of this data reader?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...