begin process at 2008 05 16 08:48:52
1 173 235 membres
76 nouveaux aujourd'hui
13 970 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

Sujet : update non effectif [ Base de données / SQL Server ] (l0r3nz1)

update non effectif le 28/04/2008 11:20:47

l0r3nz1

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...


Re : update non effectif le 30/04/2008 12:48:38

l0r3nz1
Réponse acceptée !

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 DataRow

connection.Open()

Dim dataSet As DataSet = New DataSet

adapter.Fill(dataSet, tableName)

' Code to modify data in DataSet here

odt = dataSet.Tables(tableName)

odr = odt.NewRow

odr.Item(0) =

Me .text1.Text

odr.Item(1) =

Me .text2.Text

odt.Rows.Add(odr)

' Without the SqlCommandBuilder this line would fail.

MsgBox(

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

connection.Close()

End Using

End Sub


Classé sous : table, update, dst, objetdatatable, dad

Participer à cet échange

Appels d'offres

Pub



CalendriCode

Mai 2008
LMMJVSD
   1234
567891011
12131415161718
19202122232425
262728293031 

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS