0% found this document useful (0 votes)
21 views7 pages

For Mulaire Photo

This document contains code for a C# Windows Forms application that manages user data in a MySQL database. It includes functions for retrieving, inserting, updating, and deleting user records from the database using stored procedures. The form populates a list view with user data and allows the user to select a record to view, modify, or delete. It validates the user input, opens a database connection, builds SQL commands with parameters, executes them, and handles image file storage for user photos.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

For Mulaire Photo

This document contains code for a C# Windows Forms application that manages user data in a MySQL database. It includes functions for retrieving, inserting, updating, and deleting user records from the database using stored procedures. The form populates a list view with user data and allows the user to select a record to view, modify, or delete. It validates the user input, opens a database connection, builds SQL commands with parameters, executes them, and handles image file storage for user photos.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace wCan2023MySQLProf
{
public partial class frmUtilisateurMySQL_Bis : Form
{
char optionAppel = 'A';
string DossierPhoto = @"C:\BJT_L2\wCan2023MySqlProf\PhotoJoueur\";

//déclaration d'un attribut de classe de type SqlConnection pour la


connection à la BD
MySqlConnection SqlConX = new MySqlConnection();
public frmUtilisateurMySQL_Bis()
{
InitializeComponent();
rafraichirList();
}

private void rafraichirList()


{
lviewRech.Items.Clear();
DataTable dt = SelectAll(txtFiltre.Text);
foreach (DataRow row in dt.Rows)
{
string vLogin = "";
string vNomPrenom = "";
string vEmail = "";
string vRole = "";

if (!DBNull.Value.Equals(row["UserLogin"])) vLogin =
(string)row["UserLogin"];
if (!DBNull.Value.Equals(row["UserNomPrenom"])) vNomPrenom =
(string)row["UserNomPrenom"];
if (!DBNull.Value.Equals(row["UserRole"])) vRole =
(string)row["UserRole"];
if (!DBNull.Value.Equals(row["UserEmail"])) vEmail =
(string)row["UserEmail"];

ListViewItem itm = lviewRech.Items.Add(vLogin);


itm.SubItems.Add(vNomPrenom);
itm.SubItems.Add(vRole);
itm.SubItems.Add(vEmail);
itm.Tag = vLogin;
}
lblNbre.Text = lviewRech.Items.Count.ToString() + " utilisateurs";
}
public DataTable SelectAll(string pUserNom)
{
string ConxStr = Divers.MaChaine2ConnexionMySQL();
MySqlConnection SqlConX = new MySqlConnection(ConxStr);
SqlConX.Open();
MySqlCommand cmd = new MySqlCommand("SPX_Utilisateur_SelectAll",
SqlConX);
cmd.CommandType = CommandType.StoredProcedure;
//Ajout des paramètres
MySqlParameter prm;
prm = new MySqlParameter("pUserNomPrenom", MySqlDbType.VarChar, 50);
prm.Value = pUserNom;
cmd.Parameters.Add(prm);
//récupération des données
DataTable dt;
dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
//Remplissage du datatable
da.Fill(dt);
//Fermer la connexion
SqlConX.Close();
return dt;
}
private void MiseABlanc()
{
txtLogin.Text = "";
txtMotdepasse.Text = "";
txtNom.Text = "";
cbUserRole.Text = "";
cbUserRole.SelectedIndex = -1;
txtEmail.Text = "";
picPhoto.Image = Image.FromFile(DossierPhoto + "DefaultPicture.jpg");
btnEnregistrer.Text = "&Enregistrer";
}

private void btnAjouter_Click(object sender, EventArgs e)


{
optionAppel = 'A';
this.Text = Application.ProductName + "-[Insertion d'utilisateur]";
MiseABlanc();
txtLogin.Focus();
}
public IDataReader GetObject(string pLogin)
{
IDataReader dr = null;
//Définition de la chaine de connexion
string ConxStr = Divers.MaChaine2ConnexionMySQL();
MySqlConnection SqlConX = new MySqlConnection(ConxStr);
SqlConX.Open();
//Commande
MySqlCommand cmd = new MySqlCommand("SPX_Utilisateur_Get", SqlConX);
cmd.CommandType = CommandType.StoredProcedure;
//Paramètres
MySqlParameter prm;
prm = new MySqlParameter("puserLogin", MySqlDbType.VarChar, 10);
prm.Value = pLogin;
cmd.Parameters.Add(prm);
dr = cmd.ExecuteReader();
return dr;
}

private void ChargerUtilisateur(IDataReader pDreader)


{
if (!(DBNull.Value.Equals(pDreader["UserLogin"])))
{
txtLogin.Text = (string)pDreader["UserLogin"];
}
if (!(DBNull.Value.Equals(pDreader["UserMotPass"])))
{
txtMotdepasse.Text = (string)pDreader["UserMotPass"];
}
if (!(DBNull.Value.Equals(pDreader["UserNomPrenom"])))
{
txtNom.Text = (string)pDreader["UserNomPrenom"];
}
if (!(DBNull.Value.Equals(pDreader["UserRole"])))
{
cbUserRole.Text = (string)pDreader["UserRole"];
}
if (!(DBNull.Value.Equals(pDreader["UserEmail"])))
{
txtEmail.Text = (string)pDreader["UserEmail"];
}
ChargerPhoto(txtLogin.Text);
}
private void btnModifier_Click(object sender, EventArgs e)
{
this.Text = Application.ProductName + "-[Modification d'utilisateur]";
MiseABlanc();
if (lviewRech.SelectedItems.Count > 0)
{
optionAppel = 'M';
string pLogin = (string)lviewRech.SelectedItems[0].Tag;
IDataReader dreader = GetObject(pLogin); ;
if (dreader.Read())
{
ChargerUtilisateur(dreader);
btnEnregistrer.Enabled = true;
btnEnregistrer.Text = "Ok-Modifier";
ChargerPhoto(pLogin);
}
SqlConX.Close();
}
}

private void btnConsulter_Click(object sender, EventArgs e)


{
this.Text = Application.ProductName + "-[Consultation d'utilisateur]";
MiseABlanc();
if (lviewRech.SelectedItems.Count > 0)
{
optionAppel = 'C';
string pLogin = (string)lviewRech.SelectedItems[0].Tag;
IDataReader dreader = GetObject(pLogin); ;
if (dreader.Read())
{
ChargerUtilisateur(dreader);
ChargerPhoto(pLogin);
btnEnregistrer.Enabled = false;
btnEnregistrer.Text = "Enregistrer";
}
SqlConX.Close();
}
}
private void ChargerPhoto(string pRefPhoto)
{
picPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
picPhoto.BackColor = Color.White;

if (File.Exists(DossierPhoto + pRefPhoto + ".jpg"))


{
picPhoto.Image = Image.FromFile(DossierPhoto + pRefPhoto + ".jpg");
}
else picPhoto.Image = Image.FromFile(DossierPhoto +
"DefaultPicture.jpg");
}
private void btnSupprimer_Click(object sender, EventArgs e)
{
this.Text = Application.ProductName + "-[Suppression d'utilisateur]";
MiseABlanc();
if (lviewRech.SelectedItems.Count > 0)
{
optionAppel = 'S';
string pLogin = (string)lviewRech.SelectedItems[0].Tag;
IDataReader dreader = GetObject(pLogin); ;
if (dreader.Read())
{
ChargerUtilisateur(dreader);
btnEnregistrer.Enabled = true;
btnEnregistrer.Text = "Ok-Supprimer";
}
SqlConX.Close();
}
}

private Boolean OkSaisie()


{
if (string.IsNullOrEmpty(txtLogin.Text))
{
MessageBox.Show("Login d'utilisateur attendu !",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLogin.Focus();
return false;
}
if (string.IsNullOrEmpty(txtNom.Text))
{
MessageBox.Show("Nom d'utilisateur attendu !",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtNom.Focus();
return false;
}
if (string.IsNullOrEmpty(cbUserRole.Text))
{
MessageBox.Show("Rôle d'utilisateur attendu !",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
cbUserRole.Focus();
return false;
}
return true;
}
private void AjoutParam(MySqlCommand pCmd)
{
pCmd.CommandType = CommandType.StoredProcedure;
pCmd.Parameters.AddWithValue("puserLogin", txtLogin.Text);
pCmd.Parameters.AddWithValue("puserNomPrenom", txtNom.Text);
pCmd.Parameters.AddWithValue("puserMotPass", txtMotdepasse.Text);
pCmd.Parameters.AddWithValue("puserRole", cbUserRole.Text + "");
pCmd.Parameters.AddWithValue("puserEmail", txtEmail.Text);
}
private void btnEnregistrer_Click(object sender, EventArgs e)
{
if (OkSaisie())
{
//définition de la chaine de connection
string constr = Divers.MaChaine2ConnexionMySQL();
//connexion à la base de données
MySqlConnection con = new MySqlConnection(constr);
con.Open();
MySqlCommand cmd = new MySqlCommand();
switch (optionAppel)
{
case 'A':
//Création de la commande sql
cmd = new MySqlCommand("SPX_Utilisateur_INSERT", con);
AjoutParam(cmd);
cmd.ExecuteScalar();
EnregistrerPhoto(DossierPhoto + txtLogin.Text + ".jpg");
MessageBox.Show("la saisie à été ajoutée",
Application.ProductName);
break;
case 'M':
//Création de la commande sql
cmd = new MySqlCommand("SPX_Utilisateur_UPDATE", con);
AjoutParam(cmd);
cmd.ExecuteScalar();
EnregistrerPhoto(DossierPhoto + txtLogin.Text + ".jpg");
MessageBox.Show("la modification a été enregistrée ",
Application.ProductName);
break;
case 'S':
//Création de la commande sql
cmd = new MySqlCommand("SPX_Utilisateur_Delete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("puserLogin", txtLogin.Text);
cmd.ExecuteScalar();
MessageBox.Show("la suppression a été effectuée ",
Application.ProductName);
break;
}
MiseABlanc();
rafraichirList();
SqlConX.Close();
}
}
private void EnregistrerPhoto(string pCheminPhoto)
{
if (!string.IsNullOrEmpty(txtLogin.Text))
{
picPhoto.Image.Save(pCheminPhoto,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

private void btnActualiser_Click(object sender, EventArgs e)


{
rafraichirList();
}

private void txtFiltre_TextChanged(object sender, EventArgs e)


{
rafraichirList();
}

private void btnFermer_Click(object sender, EventArgs e)


{
this.Close();
}

private void btnImage_Click(object sender, EventArgs e)


{
string dossierInitial = @"C:\BJT_L2\wCan2023MySqlProf\PhotoJoueur\";
openFileDialog1.InitialDirectory = dossierInitial;
openFileDialog1.Filter = "Fichiers image (*.jpg)|*.jpg|Tous les
fichiers (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK) // Test result.
{
txtFileName.Text = openFileDialog1.FileName;
picPhoto.Image = Image.FromFile(txtFileName.Text);
if (!string.IsNullOrEmpty(txtLogin.Text))
{
picPhoto.Image.Save(dossierInitial + txtLogin.Text,
System.Drawing.Imaging.ImageFormat.Jpeg);
}

//FileStream oImg = new FileStream(@"" + txtFileName.Text,


FileMode.Open, FileAccess.Read);
//BinaryReader oBinaryReader = new BinaryReader(oImg);
//Byte[] oImgByteArray = oBinaryReader.ReadBytes((int)oImg.Length);
//Divers.laPhoto = oImgByteArray;
//CurrentObj.PhotoUpdate(txtNumId.Text, oImgByteArray);
//oBinaryReader.Close();
//oImg.Close();
}
openFileDialog1.Dispose();
}

private void ajouterToolStripMenuItem_Click(object sender, EventArgs e)


{
btnAjouter_Click(sender, e);
}

private void modifierToolStripMenuItem_Click(object sender, EventArgs e)


{
btnModifier_Click(sender, e);
}

private void consulterToolStripMenuItem_Click(object sender, EventArgs e)


{
btnConsulter_Click(sender, e);
}

private void supprimerToolStripMenuItem_Click(object sender, EventArgs e)


{
btnSupprimer_Click(sender, e);
}

private void actualiserToolStripMenuItem_Click(object sender, EventArgs e)


{
btnActualiser_Click(sender, e);
}
}
}

You might also like