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 them 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()