IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Mettre a jour table Mysql via ODBC


Sujet :

VB.NET

  1. #1
    Membre averti
    Homme Profil pro
    Responsable Maintenance
    Inscrit en
    Août 2012
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Responsable Maintenance

    Informations forums :
    Inscription : Août 2012
    Messages : 478
    Points : 442
    Points
    442
    Par défaut Mettre a jour table Mysql via ODBC
    Bonjour à tous et à toutes,

    Je réalise un projet perso en utilisant une petite base local en MySQL.

    J'arrive a récupérer les données de la manière suivante

    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
    26
    27
     
     Public Sub ChargerDonnées()
            TableMateriel = DS1.Tables.Add("Materiel")
            TableFournisseurs = DS1.Tables.Add("Fournisseurs")
            TableMaintenance = DS1.Tables.Add("Maintenance")
            TableTravauxType = DS1.Tables.Add("Travauxtype")
     
     
            Dim Cn As New Odbc.OdbcConnection
            Dim Cnstring As String = "Dsn=localhost;uid=root"
            Cn.ConnectionString = Cnstring
            Dim Admateriel As New Odbc.OdbcDataAdapter(SQLMateriel, Cn)
            Dim AdFournisseurs As New Odbc.OdbcDataAdapter(SQLFournisseurs, Cn)
            Dim Admaintenance As New Odbc.OdbcDataAdapter(SQLMaintenance, Cn)
            Dim AdTravauxType As New Odbc.OdbcDataAdapter(SQLTravauxType, Cn)
     
            Cn.Open()
            Admateriel.Fill(TableMateriel)
            Admaintenance.Fill(TableMaintenance)
            AdTravauxType.Fill(TableTravauxType)
            AdFournisseurs.Fill(TableFournisseurs)
            Cn.Close()
     
            TableFournisseurs.PrimaryKey = New DataColumn() {TableFournisseurs.Columns("idfournisseurs")}
     
     
        End Sub
    Ensuite j' utilise un form pour ajouter dans une datatable des données de la manières suivante

    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
    26
    27
    28
    29
    30
    31
     
     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            Dim NRow As DataRow
            ' Recuperation du dernier ID
            Dim DV As DataView = TableMateriel.DefaultView
            DV.Sort = "IDMateriel ASC"
            Dim ValeurID As Integer = 0
            For Each RW As DataRowView In DV
                If ValeurID < CInt(RW.Item("IDMateriel")) Then
                    ValeurID = CInt(RW.Item("IDMateriel"))
                End If
            Next
            Dim NewID As Integer = ValeurID + 1
            NRow = TableMateriel.NewRow
            NRow(0) = NewID
            NRow(1) = TextBox1.Text
            NRow(2) = TextBox2.Text
            NRow(3) = TextBox3.Text
            NRow(4) = TextBox4.Text
            NRow(5) = TextBox5.Text
     
            If IDFournisseurtrouve <> 0 Then
                NRow(6) = IDFournisseurtrouve
            End If
     
            TableMateriel.Rows.Add(NRow)
     
     
            ' Majbase()
     
        End Sub

    Le soucis malgré mes recherche sur internet je n'arrive p as a mettre à jour la base souvent erreur SQL ou parametres je patine autour du truc mais je ne trouve pas la solution, je cherche une exemple clair
    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
     
    Public Sub Majbase()
     
            Dim SQLupdate = "UPDATE  Materiel"
            Dim SQLinsert = "INSERT INTO Materiel (IdMateriel , RéferenceMat , DesignationMat , LieuMat , ModeleMat , IDFournisseur) VALUES (?,?,?,?,?,?)"
            Dim Cn As New Odbc.OdbcConnection
            Dim Cnstring As String = "Dsn=localhost;uid=root"
            Cn.ConnectionString = Cnstring
            Dim Ad As New Odbc.OdbcDataAdapter
            Ad.UpdateCommand = New Odbc.OdbcCommand(SQLupdate, Cn)
            Ad.InsertCommand = New Odbc.OdbcCommand(SQLinsert, Cn)
            ' parametres sgbd table materiel
     
     
            Cn.Open()
     
            Ad.Update(TableMateriel)
     
            Cn.Close()
     
     
     
     
        End Sub
    Merci si une personne sait comment faire.

  2. #2
    Membre éclairé Avatar de r.morel
    Homme Profil pro
    Dessinateur CAO
    Inscrit en
    Août 2014
    Messages
    336
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Dessinateur CAO
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2014
    Messages : 336
    Points : 667
    Points
    667
    Par défaut
    Salut,
    Il y a un tutoriel complet MySQl .Net ici : http://morpheus.developpez.com/mysqldotnet/

    Même si il est en C les objets et la logique est la même.

    En espérant t'avoir aider
    Merci de ainsi que d'utiliser les boutons et

  3. #3
    Membre averti
    Homme Profil pro
    Responsable Maintenance
    Inscrit en
    Août 2012
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Responsable Maintenance

    Informations forums :
    Inscription : Août 2012
    Messages : 478
    Points : 442
    Points
    442
    Par défaut
    Bonsoir,
    Merci pour ta réponse mais j'ai déja vu et lu ce tuto.
    Je n' y connais rien en C .
    Le tuto concerne la lecture de donnée avec une requête , ca je le maîtrise déjà mon problème c'est plutôt l'écriture.

    J'y suis presque j'ai juste une erreur SQL a lever.

  4. #4
    Membre averti
    Homme Profil pro
    Responsable Maintenance
    Inscrit en
    Août 2012
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Responsable Maintenance

    Informations forums :
    Inscription : Août 2012
    Messages : 478
    Points : 442
    Points
    442
    Par défaut
    Bonjour je mets en résolu j'ai fini par y arriver en contournant le problème je mets simultanement ma datatable et ma base mysql à jour de la manière suivante

    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
    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
     
     
        Public Sub MajMateriel(ByVal IDmat As Integer, refMat As String, DesMat As String, LieuMat As String, MarQMat As String, ModMat As String, Idfou As Integer, IdMai As Integer, IdAmdec As Integer)
            ' Construction de la requête selon les variables renseignées
            Dim R1str As String = "IDmateriel =" & CStr(IDmat)
            Dim R2str As String = ""
            Dim R3str As String = ""
            Dim R4str As String = ""
            Dim R5str As String = ""
            Dim R6str As String = ""
            Dim R7str As String = ""
            Dim R8str As String = ""
            Dim R9str As String = ""
     
     
            If refMat = "" Then
                R2str = ""
            ElseIf refMat <> "" Then
                R2str = " , RéferenceMat ='" & refMat & "'"
            End If
     
            If DesMat = "" Then
                R3str = ""
            ElseIf DesMat <> "" Then
                R3str = " , DesignationMat ='" & DesMat & "'"
            End If
     
            If LieuMat = "" Then
                R4str = ""
            ElseIf LieuMat <> "" Then
                R4str = " , LieuMat ='" & LieuMat & "'"
            End If
     
            If ModMat = "" Then
                R5str = ""
            ElseIf ModMat <> "" Then
                R5str = " , ModeleMat ='" & ModMat & "'"
            End If
     
            If MarQMat = "" Then
                R6str = ""
            ElseIf MarQMat <> "" Then
                R6str = " , MarqueMat='" & MarQMat & "'"
            End If
     
            If Idfou = 0 Then
                R7str = ""
            ElseIf Idfou <> 0 Then
                R7str = " , Idfournisseur= " & CStr(Idfou)
            End If
     
            If IdMai = 0 Then
                R8str = ""
            ElseIf IdMai <> 0 Then
                R8str = " , Idmaintenance = " & CStr(IdMai)
            End If
     
            If IdAmdec = 0 Then
                R9str = ""
            ElseIf IdAmdec <> 0 Then
                R9str = " , Idamdec =" & CStr(IdAmdec)
            End If
     
     
     
            Dim Request = R1str & R2str & R3str & R4str & R5str & R6str & R7str & R8str & R9str
     
            Dim Requete As String = " INSERT INTO Materiel SET " & request
     
            Dim Slcommand As String = "SELECT * FROM Materiel"
            Dim Cn As New Odbc.OdbcConnection
            Dim Cnstring As String = "Dsn=localhost;uid=root"
            Cn.ConnectionString = Cnstring
            Dim Cmd As New Odbc.OdbcCommand(Requete, Cn)
     
     
            Cn.Open()
            Cmd.ExecuteNonQuery()
            Cn.Close()
     
     
        End Sub
    Merci à tous

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Mettre à jour table MySQL avec PHP
    Par beegees dans le forum PHP & Base de données
    Réponses: 16
    Dernier message: 16/10/2008, 13h40
  2. Réponses: 2
    Dernier message: 15/07/2008, 09h00
  3. Réponses: 3
    Dernier message: 31/01/2008, 17h48
  4. Réponses: 4
    Dernier message: 15/01/2008, 16h36
  5. Modifier une table MySQL via un lien
    Par Hayabusa dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 08/02/2006, 22h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo