|
-
Jan 6th, 2003, 10:54 PM
#1
Thread Starter
New Member
MySQL, Querying last auto-increment
Greetings and salutations!
I am still working on my application. Now... I have a form where you create a characters main values and then save
them to the MySQL-server. I need the ID for the newly created chummer, though. I tried:
Code:
Dim QueryString As String = "INSERT INTO pc VALUES (NULL,'" + txtCharName.Text + "'," + Reaction.Value.ToString + "," + Initiative.Value.ToString + "," + Perception.Value.ToString + "," + Body.Value.ToString + "," + Willpower.Value.ToString + "," + Mental.Value.ToString + "," + Physical.Value.ToString + "," + Overflow.Value.ToString + ")"
Dim QueryID As String = "SELECT * FROM 'pc' WHERE id=LAST_INSERT_ID();"
Dim myReader As OdbcDataReader
Dim myConnection As New OdbcConnection(MDIForm.MyConString)
Dim myCommand As New OdbcCommand(QueryString, myConnection)
Dim myQueryCommand As New OdbcCommand(QueryID, myConnection)
Dim id As Integer
If TestConnect(myConnection) Then
myConnection.Open()
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
myReader = myQueryCommand.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read
id = myReader.GetInt16(0)
End While
End If
myConnection.Close()
Naturally, it fails, as I try to send two Executereader() with the same connection. However, LAST_INSERT_ID()
only lasts during the connection, the next time I connect to the server, that data is lost.
I really need that ID. How should I do to query it, you think?
Note:
TestConnect() is a function I wrote to confirm that you can actually connect to the MySQL server, and have
access to the proper database. That connection closes down before the function is exited, however.
"Two wrongs won't make one right, but three left does!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|