Tp Ihm Et Bd en Vb_020446

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

TRAVAUX PRATIQUES : INTERFACES GRAPHIQUES ET BASES DE DONNEES MYSQL EN VB

1. Créer le projet
2. Insérer le driver
3. Créer l’interface

champ Propriété valeur


Textbox1 Name IdEtudiant
Textbox2 Name NomEtudiant
Textbox3 Name SexeEtudiant
Bouton1 Name Ajouter
Bouton2 Name Modifier
Bouton3 Name Supprimer
Bouton4 Name Afficher
Bouton5 Name Reinitialiser
DataGridView1 Name DataGridView1

4. Insérer le code
Imports MySql.Data.MySqlClient

Public Class Form1

Dim connStr As String =


"server=localhost;user=root;database=test;port=3306;password=;"
Dim cn As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand()
Dim n As Integer
Dim chaine, nom, sexe As String

Private Sub Ajouter_Click(sender As Object, e As EventArgs) Handles


Ajouter.Click

nom = NomEtudiant.Text
sexe = SexeEtudiant.Text
cn.Open()
cmd.Connection = cn

cmd.CommandText = String.Format("insert into etudiant(nom, sexe)


values('{0}','{1}')", nom, sexe)
cmd.ExecuteNonQuery()
cn.Close()

MessageBox.Show("Etudiant bien ajouté")

End Sub

Private Sub Modifier_Click(sender As Object, e As EventArgs) Handles


Modifier.Click

n = Val(IdEtudiant.Text)
Page 1 sur 2
nom = NomEtudiant.Text

cn.Open()
cmd.Connection = cn

cmd.CommandText = String.Format("update etudiant set nom='{0}' where


id={1}", nom, n)
cmd.ExecuteNonQuery()

MessageBox.Show("Etudiant bien modifié")

End Sub

Private Sub Supprimer_Click(sender As Object, e As EventArgs) Handles


Supprimer.Click

n = Val(IdEtudiant.Text)

cn.Open()
cmd.Connection = cn

cmd.CommandText = String.Format("delete from etudiant where id={0}", n)


cmd.ExecuteNonQuery()

MessageBox.Show("Etudiant bien supprimé")


cn.Close()

End Sub

Private Sub Afficher_Click(sender As Object, e As EventArgs) Handles


Afficher.Click
'procédure qui permet de charger le contenu de la table client dans le
Datagridview
Dim cmd As New MySqlCommand()
cn.Open()
cmd.Connection = cn
cmd.CommandText = "select * from etudiant"
Dim dr As MySqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable()
dt.Load(dr)
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt
dr.Close()
cn.Close()
End Sub

Private Sub Reinitialiser_Click(sender As Object, e As EventArgs) Handles


Reinitialiser.Click
IdEtudiant.Text = ""
NomEtudiant.Text = ""
SexeEtudiant.Text = ""
End Sub
End Class

Page 2 sur 2

You might also like