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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
Imports System.Data.OleDb
Public Class Form3
Dim cheminBdCentrale As String = "C:\dossier1\BdCentrale.mdb"
Dim cheminBdSecteur As String = "C:\dossier2\BdSecteur.mdb"
'Dim cheminBdCentrale As String = My.Settings.BDCENTRALECnStr
'Dim cheminBdSecteur As String = My.Settings.BDSECTEURCnStr
Dim mdp As String = "kika"
'ajouter un textbox pour saisir le password pour la bd source (secteur)
'pas besoin pour la bd destination(bd centrale)
'
'Button btnOpenFile
Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
Dim dlg1 As New OpenFileDialog
'etc....pour recuperer cheminBdCentrale
Dim dlg2 As New OpenFileDialog
'etc....pour recuperer cheminBdSecteur
End Sub
Private Sub btnImportData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportData.Click
If cheminBdCentrale Is String.Empty Or cheminBdSecteur Is String.Empty Or mdp Is String.Empty Then
Exit Sub
End If
'variable Centrale
Dim centralDS As New DataSet
Dim centralDT As New DataTable
Dim centralDA As New OleDb.OleDbDataAdapter
Dim centralCommand As New OleDbCommand()
'CommandBuilder necessaire pour repercuter modif
' du dataset centralDS dans BD .
Dim builderCentral As OleDbCommandBuilder = _
New OleDbCommandBuilder(centralDA)
'variable Secteur
Dim secteurDS As New DataSet
Dim secteurDT As New DataTable
Dim secteurDA As New OleDb.OleDbDataAdapter
Dim secteurCommand As New OleDbCommand()
'Connection à BDCENTRALE
'------------------------
Dim conCentrale As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Me.cheminBdCentrale)
conCentrale.Open()
centralCommand.CommandText = "SELECT * FROM BDCENTRALE;"
centralCommand.Connection = conCentrale
centralDA.SelectCommand = centralCommand
centralDA.Fill(centralDS, "Destination")
centralDT = centralDS.Tables(0)
'Connection à BDSECTEUR
'------------------------
Dim ConSecteur As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Me.cheminBdSecteur)
ConSecteur.Open()
secteurCommand.CommandText = "SELECT * FROM [SECTEUR1];"
secteurCommand.Connection = ConSecteur
secteurDA.SelectCommand = secteurCommand
secteurDA.Fill(secteurDS, "Source")
secteurDT = secteurDS.Tables(0)
'L'AJOUT ( "Lignes nouvelles ") à DataTable Centrale
'1-CREER UN DATATABLE TEMPORAIRE( meme structure & vide) QUE secteurDT
Dim nouvDataTable As DataTable = secteurDT.Clone
Dim dr As DataRow
'2- DEFINIR DES CLES PRIMAIRES AVEC 2 CHAMPS centralDT
Dim centralCle(1) As DataColumn
centralCle(0) = centralDT.Columns(0)
centralCle(1) = centralDT.Columns(1)
centralDT.PrimaryKey = centralCle
'3-POPULATE nouvDataTable
For Each ligneSource As DataRow In secteurDT.Rows
'le champ cle devient helas un tableau
Dim champCle() As Object = New Object(1) {}
champCle(0) = ligneSource.Item(0)
champCle(1) = ligneSource.Item(1)
'si la ligne avec la cle n'existe pas,l'ajouter
If (centralDT.Rows.Find(champCle) Is Nothing) Then
'cree nouvel. ligne dans nouvDataTable
dr = nouvDataTable.NewRow
dr.ItemArray = ligneSource.ItemArray.Clone
'MessageBox.Show(dr(0).ToString & dr(1).ToString & dr(2).ToString)
nouvDataTable.Rows.Add(dr)
End If
Next
'AFFICHE LE nouvDataTable
Me.DataGridView2.DataSource = nouvDataTable
'4-AJOUTE LE nouvDataTable à centralDS
For Each ligneSource As DataRow In nouvDataTable.Rows
Dim newRow() As Object = ligneSource.ItemArray
Call AjoutMAJ(centralDT, newRow)
Next
'SUPPRESSION
'1-AJOUTER DEFINITION DES CLES PRIMAIRES AVEC 2 CHAMPS secteurDT
Dim secteurCle(1) As DataColumn
secteurCle(0) = secteurDT.Columns(0)
secteurCle(1) = secteurDT.Columns(1)
secteurDT.PrimaryKey = secteurCle
'2-RECHERCHE LES LIGNE A SUPPRIMER
'BERRAH IGOUL OUI AOUED KIKA: on ne peut pas supprimer un element collection
' à l 'interieur d'une boucle For Each...Next .c'est pour ca la liste.
Dim lstRowSuppr As List(Of DataRow) = New List(Of DataRow)
For Each ligneCible As DataRow In centralDT.Rows
Dim champCle() As Object = New Object(1) {}
champCle(0) = ligneCible.Item(0)
champCle(1) = ligneCible.Item(1)
'si ligne avec cle n'existe pas dans secteurDT
If Not secteurDT.Rows.Contains(champCle) Then
'memorise ligne centralDT
lstRowSuppr.Add(ligneCible)
MessageBox.Show("ligne deleted :" & ligneCible(0).ToString & ligneCible(1).ToString & ligneCible(2).ToString)
End If
Next
'2-SUPPRESSION DES LIGNES DANS centralDT
For Each ligneSuppr In lstRowSuppr
centralDT.Rows.Remove(ligneSuppr)
Next
centralDS.AcceptChanges()
' CODE POUR REPERCUTER MODIF DANS LA BD(fichier disque) .
' car le dataset & datatable sont des tableaux en memoire seulement
centralDA.Update(centralDS, "Destination")
MsgBox("Data updated")
DataGridView1.DataSource = centralDS
DataGridView1.DataMember = centralDS.Tables(0).TableName
conCentrale.Close()
ConSecteur.Close()
End Sub
' Sub AjouteEtMetAjour
Private Sub AjoutMAJ(ByVal table As DataTable, ByVal newRow() As Object)
Dim row As DataRow
table.BeginLoadData()
' Add the new row to the rows collection.
row = table.LoadDataRow(newRow, True)
table.EndLoadData()
End Sub |
Partager