mysql transaction (commit and rollback)

前端 未结 2 1205
感情败类
感情败类 2021-01-01 06:29

below is the code i am using for MySqlTransaction and it is running perfectly.... but problem is as i am using single instance of mysqlcommand i have to use unique PARAMETER

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-01 07:25

    To execute multiple commands within the same transaction, ensure that you assign the transaction object to each command individually:

    Dim selectCmd As MySqlCommand = con.CreateCommand()
    Dim insertCmd As MySqlCommand = con.CreateCommand()
    
    selectCmd.CommandText = "SELECT ..."
    insertCmd.CommandText = "INSERT ..."
    
    Dim sqlTran As MySqlTransaction = con.BeginTransaction()
    Try
      selectCmd.Transaction = sqlTran
      insertCmd.Transaction = sqlTran
    
      ...selectCmd.ExecuteScalar()...
      ...insertCmd.ExecuteNonQuery()...
    
      sqlTran.Commit()
    Catch
      sqlTran.Rollback()
    End Try
    

    As others have mentioned, it is generally a good idea to Dispose() objects (that are IDisposable) as soon as you're done working with them. After disposing objects, they can no longer be used.

提交回复
热议问题