Code VBA
Code VBA
MySqlClient
Try
If LoadImagesStr = False Then
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT id, nom, adresse, ville,
departement, photo FROM " & Table_Name & " ORDER BY nom"
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
Data = MySQLDA.Fill(DT)
If Data > 0 Then
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = DT
DataGridView1.Columns(2).DefaultCellStyle.Format = "c"
DataGridView1.DefaultCellStyle.ForeColor = Color.Black
DataGridView1.ClearSelection()
Else
DataGridView1.DataSource = DT
End If
Else
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT photo FROM " & Table_Name & " WHERE
id LIKE '" & IDRam & "'"
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
Data = MySQLDA.Fill(DT)
If Data > 0 Then
Dim ImgArray() As Byte = DT.Rows(0).Item("photo")
Dim lmgStr As New System.IO.MemoryStream(ImgArray)
PictureBoxImagePreview.Image = Image.FromStream(lmgStr)
PictureBoxImagePreview.SizeMode = PictureBoxSizeMode.Zoom
lmgStr.Close()
End If
LoadImagesStr = False
End If
Catch ex As Exception
MsgBox("Impossible de charger la base de données !!!" & vbCr &
ex.Message, MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
Return
End Try
DT = Nothing
Connection.Close()
End Sub
Try
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT * FROM " & Table_Name & " WHERE id LIKE
'" & LabelID.Text.Substring(5, LabelID.Text.Length - 5) & "'"
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
Data = MySQLDA.Fill(DT)
If Data > 0 Then
Dim ImgArray() As Byte = DT.Rows(0).Item("photo")
Dim lmgStr As New System.IO.MemoryStream(ImgArray)
PictureBoxUserImage.Image = Image.FromStream(lmgStr)
lmgStr.Close()
LabelID.Text = "ID : " & DT.Rows(0).Item("id")
LabelName.Text = DT.Rows(0).Item("nom")
LabelAddress.Text = DT.Rows(0).Item("adresse")
LabelCity.Text = DT.Rows(0).Item("ville")
LabelDepartment.Text = DT.Rows(0).Item("departement")
Else
MsgBox("ID non trouvé !!!" & vbCr & "Veuillez enregistrer votre
pièce d'identité.", MsgBoxStyle.Information, "Message d'informations")
End If
Catch ex As Exception
MsgBox("Impossible de charger la base de données !!!" & vbCr &
ex.Message, MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
Return
End Try
DT = Nothing
Connection.Close()
End Sub
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connexion échouée !!!" & vbCrLf & "Veuillez
vérifier que le serveur est prêt !!!", "Message d'erreur", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
End Try
Try
MySQLCMD = New MySqlCommand
With MySQLCMD
.CommandText = "INSERT INTO " & Table_Name & " (id, nom,
adresse, ville, departement, photo) VALUES (@id, @nom, @adresse, @ville,
@departement, @photo)"
.Connection = Connection
.Parameters.AddWithValue("@nom", TextBoxNom.Text)
.Parameters.AddWithValue("@id", LabelGetID.Text)
.Parameters.AddWithValue("@adresse", TextBoxadresse.Text)
.Parameters.AddWithValue("@ville", TextBoxVille.Text)
.Parameters.AddWithValue("@departement", TextBoxDep.Text)
.Parameters.AddWithValue("@photo", arrImage)
.ExecuteNonQuery()
End With
MsgBox("Données sauvegardées avec succès", MsgBoxStyle.Information,
"Information")
IMG_FileNameInput = ""
ClearInputUpdateData()
Catch ex As Exception
MsgBox("Les données n'ont pas été sauvegardées !!!" & vbCr &
ex.Message, MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
Return
End Try
Connection.Close()
Else
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connexion échouée !!!" & vbCrLf & "Veuillez
vérifier que le serveur est prêt !!!", "Message dùerreur", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
End Try
Try
MySQLCMD = New MySqlCommand
With MySQLCMD
.CommandText = "UPDATE " & Table_Name & " SET
id=@id,nom=@nom,adresse=@adresse,ville=@ville,departement=@departement,photo=@photo
WHERE id=@id "
.Connection = Connection
.Parameters.AddWithValue("@nom", TextBoxNom.Text)
.Parameters.AddWithValue("@id", LabelGetID.Text)
.Parameters.AddWithValue("@adresse", TextBoxadresse.Text)
.Parameters.AddWithValue("@ville", TextBoxVille.Text)
.Parameters.AddWithValue("@departement", TextBoxDep.Text)
.Parameters.AddWithValue("@photo", arrImage)
.ExecuteNonQuery()
End With
MsgBox("Données mises à jour avec succès",
MsgBoxStyle.Information, "Information")
IMG_FileNameInput = ""
buttonSave.Text = "Enregistrer"
ClearInputUpdateData()
Catch ex As Exception
MsgBox("Les données n'ont pas été mises à jour !!!" & vbCr &
ex.Message, MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
Return
End Try
Connection.Close()
Else
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connexion échouée !!!" & vbCrLf & "Veuillez
vérifier que le serveur est prêt !!!", "Message d'erreur", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
End Try
Try
MySQLCMD = New MySqlCommand
With MySQLCMD
.CommandText = "UPDATE " & Table_Name & " SET
id=@id,nom=@nom,adresse=@adresse,ville=@ville,departement=@departement WHERE id=@id
"
.Connection = Connection
.Parameters.AddWithValue("@nom", TextBoxNom.Text)
.Parameters.AddWithValue("@id", LabelGetID.Text)
.Parameters.AddWithValue("@adresse", TextBoxadresse.Text)
.Parameters.AddWithValue("@ville", TextBoxVille.Text)
.Parameters.AddWithValue("@departement", TextBoxDep.Text)
.ExecuteNonQuery()
End With
MsgBox("Données mises à jour avec succès",
MsgBoxStyle.Information, "Information")
buttonSave.Text = "Enregistrer"
ClearInputUpdateData()
Catch ex As Exception
MsgBox("Les données n'ont pas été mises à jour !!!" & vbCr &
ex.Message, MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
Return
End Try
Connection.Close()
End If
StatusInput = "Enregistrer"
End If
PictureBoxImagePreview.Image = Nothing
ShowData()
End Sub
If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK)
Then
IMG_FileNameInput = OpenFileDialog1.FileName
PictureBoxImageInput.ImageLocation = IMG_FileNameInput
End If
End Sub
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connexion échouée !!!" & vbCrLf & "Veuillez vérifier
que le serveur est prêt !!!", "Message d'erreur", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
End Try
Try
MySQLDA = New MySqlDataAdapter(SqlCmdSearchstr, Connection)
DT = New DataTable
Data = MySQLDA.Fill(DT)
If Data > 0 Then
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = DT
DataGridView1.DefaultCellStyle.ForeColor = Color.Black
DataGridView1.ClearSelection()
Else
DataGridView1.DataSource = DT
End If
Catch ex As Exception
MsgBox("La recherche a échoué." & vbCr & ex.Message,
MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
End Try
Connection.Close()
End Sub
If DataGridView1.SelectedRows.Count = 0 Then
MsgBox("Impossible à supprimer, sélectionnez les données de la table à
supprimer.", MsgBoxStyle.Critical, "Message d'erreur")
Return
End If
Try
If AllCellsSelected(DataGridView1) = True Then
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "DELETE FROM " & Table_Name
MySQLCMD.Connection = Connection
MySQLCMD.ExecuteNonQuery()
End If
Try
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT * FROM " & Table_Name & " WHERE id LIKE
'" & LabelGetID.Text & "'"
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
Data = MySQLDA.Fill(DT)
If Data > 0 Then
If MsgBox("ID enregistrée !" & vbCr & "Vous souhaitez modifier les
données ?", MsgBoxStyle.Question + MsgBoxStyle.OkCancel, "Confirmation") =
MsgBoxResult.Cancel Then
DT = Nothing
Connection.Close()
buttonScanID.Enabled = True
GetID = False
LabelGetID.Text = "________"
Return
Else
Dim ImgArray() As Byte = DT.Rows(0).Item("photo")
Dim lmgStr As New System.IO.MemoryStream(ImgArray)
PictureBoxImageInput.Image = Image.FromStream(lmgStr)
PictureBoxImageInput.SizeMode = PictureBoxSizeMode.Zoom
TextBoxNom.Text = DT.Rows(0).Item("nom")
TextBoxadresse.Text = DT.Rows(0).Item("adresse")
TextBoxVille.Text = DT.Rows(0).Item("ville")
TextBoxDep.Text = DT.Rows(0).Item("departement")
StatusInput = "Mettre à jour"
End If
End If
Catch ex As Exception
MsgBox("Impossible de charger la base de données !!!" & vbCr &
ex.Message, MsgBoxStyle.Critical, "Message d'erreur")
Connection.Close()
Return
End Try
DT = Nothing
Connection.Close()
buttonScanID.Enabled = True
GetID = False
End Sub