I have one multi-line INSERT statement (300 or so sets of values) that I would like to commit to the MySQL database on an all-or-nothing basis.
insert into table VALUES
(1, 2, 3),
(4, 5, 6),
(7, 8, 9);
In some cases, the set of values in the command will not meet the table criteria (for example, a duplicate key). When this happens, I do not want any of the previous sets added to the database. I implemented this with the following code, however my rollback command does not seem to matter. I used this documentation: http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqltransaction.html
Dim transaction As MySqlTransaction = sqlConnection.BeginTransaction()
sqlCommand = New MySqlCommand(insertStr, sqlConnection, transaction)
Try
sqlCommand.ExecuteNonQuery()
Catch ex As Exception
writeToLog("EXCEPTION: " & ex.Message & vbNewLine)
writeToLog("Could not execute " & sqlCmd & vbNewLine)
Try
transaction.Rollback()
writeToLog("All statements were rolled back." & vbNewLine)
Return False
Catch rollbackEx As Exception
writeToLog("EXCEPTION: " & rollbackEx.Message & vbNewLine)
writeToLog("All statements were not rolled back." & vbNewLine)
Return False
End Try
End Try
transaction.commit()
DUPLICATE KEY, , , . ?