Bonjour à tous,
Je veux mettre à jour 2 champs CH1 et CH2 dans une table TBTEST d'une base Access 2000.
Ci-dessous le code pour le remplissage de la table TBTEST dans une datatable d'un dataset :
J'ai essayé avec 2 méthodes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 Public Sub Modifier_TBTEST(ByVal CH_CONNEXION As String) ' Déclaration Dim maCommande As New OleDbCommand() Dim maConnection As New OleDbConnection() Dim monDataAdapter As New OleDbDataAdapter() Dim monDataSet As New DataSet() Dim DR_TB As DataRow Dim CH1 As Integer Dim RSQL As String Try ' Connection maConnection.ConnectionString = CH_CONNEXION maConnection.Open() RSQL = "SELECT TBTEST.ID, TBTEST.CH1, TBTEST.CH2 FROM TBTEST" monDataAdapter.SelectCommand = New OleDbCommand(RSQL, maConnection) monDataAdapter.Fill(monDataSet, "TBTEST")
Méthode 1 : Exécution de 8000 commandes de mises à jour
Avec la méthode 1 la mise à jour prend 7 minutes pour mettre à jour 2 champs dans une table de 8000 enregistrements !!!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 CH1 = 0 For Each DR_TB In monDataSet.Tables("TBTEST").Rows CH1 = CH1 + 1 RSQL = "UPDATE TBTEST SET TBTEST.CH1 = " & CH1 RSQL = RSQL & ", TBTEST.[CH2] = 'test modif CH2'" RSQL = RSQL & " WHERE (TBTEST.ID LIKE '" & DR_TB("ID") & "')" ' Commande maCommande.Connection = maConnection maCommande.CommandText = RSQL maCommande.ExecuteNonQuery() Next DR_TB
Méthode 2 : Utilisation de la fonction UpdateCommand d'un DataAdapter
Avec cette méthode aucune mise à jour n’est effectuée et je n’ai pas de message d’erreur.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 CH1 = 0 For Each DR_TB In monDataSet.Tables("TBTEST").Rows CH1 = CH1 + 1 DR_TB("CH1") = CH1 DR_TB("CH2") = "test modif CH2" Next DR_TB monDataSet.Tables("TBTEST").AcceptChanges() Dim CmdBuild As OleDbCommandBuilder CmdBuild = New OleDb.OleDbCommandBuilder(monDataAdapter) CmdBuild.GetDeleteCommand() CmdBuild.GetInsertCommand() CmdBuild.GetUpdateCommand() 'DELETE COMMANDE = DELETE FROM TBTEST WHERE ( (ID = ?) AND ((? = 1 AND CH1 IS NULL) OR (CH1 = ?)) AND ((? = 1 AND CH2 IS NULL) OR (CH2 = ?)) ) 'DELETE INSERT = INSERT INTO TBTEST( ID , CH1 , CH2 ) VALUES ( ? , ? , ? ) 'DELETE UPDATE = UPDATE TBTEST SET ID = ? , CH1 = ? , CH2 = ? WHERE ( (ID = ?) AND ((? = 1 AND CH1 IS NULL) OR (CH1 = ?)) AND ((? = 1 AND CH2 IS NULL) OR (CH2 = ?)) ) monDataAdapter.DeleteCommand = CmdBuild.GetDeleteCommand() monDataAdapter.InsertCommand = CmdBuild.GetInsertCommand() monDataAdapter.UpdateCommand = CmdBuild.GetUpdateCommand() monDataAdapter.Update(monDataSet.Tables("TBTEST"))
Avez-vous des pistes ?
Merci d'avance pour vos réponses
Partager