1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
   |  
  Try
      oleCon.Open()
      Dim trans As OleDbTransaction = oleCon.BeginTransaction()
 
      ' Transfert des données de la table Category
      Dim strCategory As String = "SELECT Category.ID, Category.Label FROM Category WHERE Category.ID = 1"
      Using olecmd As New OleDbCommand(strCategory, oleCon, trans)
          Using dreaderCategory As OleDbDataReader = olecmd.ExecuteReader()
              If dreaderCategory.Read() Then
                  Dim intID As Integer = dreaderCategory("ID")
                  Dim strLabel As String = dreaderCategory("Label")
 
                  Dim strCategory_2 As String = "INSERT INTO Category_2 (ID, Label) VALUES (@ID, @Label)"
                  Using olecomCategory_2 As New OleDbCommand(strCategory_2, oleCon, trans)
                      olecomCategory_2.Parameters.AddWithValue("@ID", intID)
                      olecomCategory_2.Parameters.AddWithValue("@Label", strLabel)
                      olecomCategory_2.ExecuteNonQuery()
                  End Using
              End If
          End Using
      End Using
 
      ' Transfert des données de la table Product
      Dim strProduct As String = "SELECT Product.ID, Product.Label, Product.ID_Cat FROM Product WHERE Product.ID_Cat = 1"
      Using olecmdProduct As New OleDbCommand(strProduct, oleCon, trans)
          Using dreaderProduct As OleDbDataReader = olecmdProduct.ExecuteReader()
              Dim strProduct_2 As String = "INSERT INTO Product_2 (ID, Label, ID_Cat) VALUES (@ID, @Label, @ID_Cat)"
              Using olecomProduct_2 As New OleDbCommand(strProduct_2, oleCon, trans)
                  While dreaderProduct.Read()
                      olecomProduct_2.Parameters.Clear()
                      olecomProduct_2.Parameters.AddWithValue("@ID", dreaderProduct("ID"))
                      olecomProduct_2.Parameters.AddWithValue("@Label", dreaderProduct("Label"))
                      olecomProduct_2.Parameters.AddWithValue("@ID_Cat", dreaderProduct("ID_Cat"))
                      olecomProduct_2.ExecuteNonQuery()
                  End While
              End Using
          End Using
      End Using
 
      ' Commit de la transaction
      trans.Commit()
  Catch ex As Exception
      ' Rollback de la transaction en cas d'erreur
      If oleCon.State = ConnectionState.Open Then
          trans.Rollback()
      End If
      MessageBox.Show("Erreur : " & ex.Message)
  Finally
      ' Fermeture de la connexion
      If oleCon.State = ConnectionState.Open Then
          oleCon.Close()
      End If
  End Try | 
Partager