Modification de DataSet

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - Modifié par jordane45 le 19/04/2015 à 22:37
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 21 avril 2015 à 10:51
Bonjour le Forum,

Avec deux datasets:

Le premier:
MyDataSet = New DataSet
MyDataSet.Tables.Add("Trames")
MyDataSet.Tables("Trames").Columns.Add("Trame CGN", GetType(String))
MyDataSet.Tables("Trames").Columns.Add("File", GetType(String))

For Each foundFile As String In My.Computer.FileSystem.GetFiles(MyPathTrames)
    Dim lastpart As String = foundFile.Substring(foundFile.LastIndexOf("\") + 1)
    Dim item As String() = lastpart.Split(New Char() {New String(" "c, 1)}, StringSplitOptions.None)
    MyDataSet.Tables("Trames").Rows.Add(item(0), foundFile)
Next foundFile

J'ai donc décomposé le nom d'un fichier Template pour avoir:
champ Trame = TR1
champ File = "\\serveur\folder\TR1 - xxxxxx.docx"

Le deuxième:
' Construction de la requête SQL
For i As Integer = 0 To ArrayListItem.Count - 1
    sqlPDP = sqlPDP & ArrayListItem(i) & ","
Next
sqlPDP = sqlPDP.Substring(0, sqlPDP.Length - 1)

MyQuery = " SELECT " & sqlPDP & "" & _
          " FROM [Jobs$] " & _
          " WHERE [Trame CGN] IS NOT NULL AND [Job] IS NOT NULL "
da = New OleDb.OleDbDataAdapter(MyQuery, MyConnexion)
da.Fill(MyDataSet, "Consignes")
MyConnexion.Close()


Dans Tables("Consignes"), il y a un champ [Trame CGN] contenant TR1, ou TR2, ou TR11 etc.

Je souhaiterai modifier le contenu de ce champ par le contenu du champ "File" correspondant au champ "Trame CGN" de Tables("Trames").

Quelle méthode pouvez-vous me conseiller ?

Merci de vos suggestions,
jean-marc

2 réponses

cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
20 avril 2015 à 19:58
Bonsoir le Forum,

Avec le code :
Dim trames As DataTable = MyDataSet.Tables("Trames")

For i As Integer = trames.Rows.Count - 1 To 0 Step -1
Dim foundTrames As DataRow()

foundTrames = MyDataSet.Tables("Consignes").Select("[Trame CGN] Like '" & trames(i)("Trame CGN") & "'")

If foundTrames IsNot Nothing Then
Me.RichTextBox1.AppendText(dt(MyDt) & Environment.NewLine)
Me.RichTextBox1.AppendText(dt(MyDt) & Environment.NewLine)
Me.RichTextBox1.AppendText(dt(MyDt) & "Trame trouvée " & New String(" "c, 3) & _
trames(i)("Trame CGN").ToString & New String(" "c, 3) & _
trames(i)("File").ToString)
Me.RichTextBox1.AppendText(dt(MyDt) & "foundTrames.Length : " & foundTrames.Length)

For k As Integer = 0 To foundTrames.Length - 1
Me.RichTextBox1.AppendText(dt(MyDt) & "boucle k " & New String(" "c, 3) & _
foundTrames(k)(0) & New String(" "c, 3) & _
foundTrames(k)(1) & New String(" "c, 3) & _
foundTrames(k)(2) & New String(" "c, 3) & _
foundTrames(k)(3) & New String(" "c, 3) & _
foundTrames(k)("Trame CGN") & New String(" "c, 3) & _
trames(i)("File").ToString)

MyDataSet.Tables("Consignes").Rows(k).Item("Trame CGN") = trames(i)("File").ToString

System.Windows.Forms.Application.DoEvents()
Next
End If
Next
Me.RichTextBox1.AppendText(dt(MyDt) & "fin modif Table Consignes")

Me.RichTextBox1.AppendText(dt(MyDt) & "Contrôle new Table Consignes")
For Each row As DataRow In MyDataSet.Tables("Consignes").Rows
Me.RichTextBox1.AppendText(dt(MyDt) & row(2) & New String(" "c, 3) & _
row(3) & New String(" "c, 3) & _
row("Trame CGN").ToString)
System.Windows.Forms.Application.DoEvents()
Next


La boucle k m'affiche correctement le fichier "Trame CGN" en remplacement de TR1, TR2 ....

Je parcours la table par la fin car TR1, TR2, ... TR11... TR21 à cause du Like dans le Select.

Par contre, lors du contrôle de la table modifiée, celle-ci l'est correctement au début puis patatras !!!
J'ai ajouté un tri pour tester mais nenni.


D'où pourrait venir mon erreur et est-ce la bonne méthode ???

Merci de vos suggestions,
jean-marc




jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
21 avril 2015 à 10:51
Bonjour le Forum,

N'ayant pas abouti avec le Select, j'ai utilisé le Find.

Dans ma Table "Trames", j'ai défini la colonne "Trame CGN" comme clé primaire.
MyDataSet = New DataSet
MyDataSet.Tables.Add("Trames")
MyDataSet.Tables("Trames").Columns.Add("Trame CGN", GetType(String))
MyDataSet.Tables("Trames").Columns.Add("File", GetType(String))

Dim columns(1) As DataColumn
columns(0) = MyDataSet.Tables("Trames").Columns("Trame CGN")
MyDataSet.Tables("Trames").PrimaryKey = columns

For Each foundFile As String In My.Computer.FileSystem.GetFiles(MyPathTrames)
Dim lastpart As String = foundFile.Substring(foundFile.LastIndexOf("\") + 1)
Dim item As String() = lastpart.Split(New Char() {New String(" "c, 1)}, StringSplitOptions.None)
MyDataSet.Tables("Trames").Rows.Add(item(0), foundFile)
Next foundFile

Puis pour remplacer le champ TRx dans la Table "Consignes" par le nom complet du fichier TRx, j'ai utilisé le Find.
For Each row As DataRow In MyDataSet.Tables("Consignes").Rows
Dim foundRow As DataRow = MyDataSet.Tables("Trames").Rows.Find(row("Trame CGN").ToString)
row("Trame CGN") = foundRow(1).ToString()
Next


Problème résolu.

jean-marc
0
Rejoignez-nous