我有一个多行INSERT语句(约300个值集),我想以全有或全无的方式提交给MySQL数据库.
insert into table VALUES
(1,2,3),(4,5,6),(7,8,9);
在某些情况下,命令中的一组值将不符合表的条件(例如,重复键).发生这种情况时,我不希望将任何先前的集合添加到数据库中.我已经用以下代码实现了这一点,但是,我的rollback命令似乎没有什么不同.我使用了以下文档: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异常,没有Rollback Exception异常,并且每组值都被重复提交给数据库.我究竟做错了什么?
最佳答案