Update non effectif

Résolu
l0r3nz1 Messages postés 218 Date d'inscription mercredi 20 février 2008 Statut Membre Dernière intervention 17 mars 2012 - 28 avril 2008 à 11:20
l0r3nz1 Messages postés 218 Date d'inscription mercredi 20 février 2008 Statut Membre Dernière intervention 17 mars 2012 - 30 avril 2008 à 12:48
Bonjour, apparemment j'ai un souci avec le vb2005, pourtant j'ai l'impression d'avoir le bon code (vu les questions sur forum) mais voila quelqu'un voit il ou est la faute svp, je n'ai que le message "maj = 0" pour zéro mise à jour au lieu de 3      


'******   declarations                 ******************************
Dim con As SqlConnection ' objet conexion à la BD
Dim com As SqlCommand ' objet commande
Dim dad As SqlDataAdapter ' objet DataAdapter
Dim dst As DataSet ' bjet DataSet
Dim ObjetDataTable As DataTable
Dim objetDataRow As DataRow
Dim bdd
Dim table
Dim champ
Dim chainCom, txt1 As String
Dim comb As New SqlClient.SqlCommandBuilder(dad)


'************    assigner quleques valeurs        *************************
txt1 = Me.text1.Text
bdd = "C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\mission.mdf"
table = "recupData"
champ = "commentaire"
con = New SqlConnection("Server=" & My.Computer.Name & "\sqlexpress;Data
con.Open()
Source=.\SQLExpress;Integrated Security=true;AttachDbFilename= " & bdd & ";")
com = New SqlCommand
com.Connection = con
com.CommandType = CommandType.Text
com.CommandText = "SELECT * FROM " & table
dad = New SqlDataAdapter(com)
dst = New DataSet
dst.Clear() ' Vider le Dataset avnt son remplissage
dad.Fill(dst, table) ' Remplissage du Dataset


dst.AcceptChanges()
dst.Tables(0).Rows(5).Item(1) = "mmm"
dst.Tables(0).Rows(4).Item(0) = Me.text1
dst.Tables(0).Rows(3).Item(1) = Me.text2
'MsgBox(dst.Tables(0).Rows(5).Item(1).ToString) ' ca donne 'mmm' ici donc c'est bon
       
        comb.DataAdapter = dad
        Try
            dad.InsertCommand = comb.GetInsertCommand
            dad.DeleteCommand = comb.GetDeleteCommand
            dad.UpdateCommand = comb.GetUpdateCommand
        Catch ex As Exception
            Debug.Print(ex.ToString)
        End Try
 
        Try
            If dst.HasChanges Then
                dad.ContinueUpdateOnError = True
                dad.Update(dst, table)
                MsgBox(" maj : " & dad.Update(dst, table))
            Else
                MsgBox("aucun changement")
            End If


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
con.Close()




        '******************         test 2  pas conection et utilisation object row             ************************


con.Open()


        com.CommandText = "SELECT * FROM " & table
        dad = New SqlDataAdapter(com)
        dst.Clear()
        dad.Fill(dst, table)
       
        ' Extraire l'enregistrement courant
        ObjetDataTable = dst.Tables(table)
        objetDataRow = ObjetDataTable.NewRow
        objetDataRow("commentaire") = Me.text1
        ObjetDataTable.Rows.Add(objetDataRow)
        objetDataRow = ObjetDataTable.NewRow
        objetDataRow.Item(0) = Me.text2
        ObjetDataTable.Rows.Add(objetDataRow)


        'Modifier les valeurs des champs en récupérant le contenu des TextBox
        'Pour modifier les valeurs changées dans le DataAdapter
        'Mise à jour
        comb = New SqlClient.SqlCommandBuilder(dad)




        Try
            If dst.HasChanges Then
                dad.ContinueUpdateOnError = True
                dad.Update(dst, table)
                dad.Update(ObjetDataTable)
                'MsgBox(" maj : " & dad.Update(dst, table))
                MsgBox(" maj : " & dad.Update(ObjetDataTable))
            Else
                MsgBox("aucun changement")
            End If


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try




        'On vide le DataSet et on le 'recharge' de nouveau.
        dst.Clear()
con.close()


'*********************************************************************


donc voila je suis certain qu'il y a un pas grand chose mais si l'un d'entre vous a le courage ou l'envie de jeter un coup d'oeil dans ce petit bout de code...

1 réponse

l0r3nz1 Messages postés 218 Date d'inscription mercredi 20 février 2008 Statut Membre Dernière intervention 17 mars 2012
30 avril 2008 à 12:48
En fait il faut une clef primaire ( sinon c'est possible mais un peu plus compliqué) la procedure ci dessous est ok!!!

Private



Sub
modifAndAdd(

ByVal
connectionString

As



String
, _


ByVal
queryString

As



String
,

ByVal
tableName

As



String
)


Using
connection

As



New
SqlConnection(connectionString) 


Dim
adapter

As



New
SqlDataAdapter()adapter.SelectCommand =


New
SqlCommand(queryString, connection)


Dim
builder

As
SqlCommandBuilder =

New
SqlCommandBuilder(adapter)


Dim
odt

As



New
DataTable


'Dim odr As New DataRow






Dim
odr

As
DataRowconnection.Open()


Dim
dataSet

As
DataSet =

New
DataSetadapter.Fill(dataSet, tableName)


' Code to modify data in DataSet here

odt = dataSet.Tables(tableName)


odr = odt.NewRow


odr.Item(0) =




Me
.text1.Textodr.Item(1) =


Me
.text2.Textodt.Rows.Add(odr)


' Without the SqlCommandBuilder this line would fail.

MsgBox(




"nb modif = "
& adapter.Update(dataSet, tableName))connection.Close()


End



Using






End



Sub
3
Rejoignez-nous