Enregistrer une image

Signaler
Messages postés
44
Date d'inscription
lundi 27 août 2007
Statut
Membre
Dernière intervention
1 novembre 2016
-
Messages postés
14770
Date d'inscription
vendredi 14 mars 2003
Statut
Modérateur
Dernière intervention
27 février 2021
-
Bonjour,
Je souhaiterai enregistrer une image dans une base de données ACCESS. Habituellement j'enregistre l'image elle même dans la base comme ceci:
        private void Click_Photo(object sender, EventArgs e)
{
try
{
// Nom du fichier de la photo à ajouter
OpenFileDialog ofdImage = new OpenFileDialog();
ofdImage.Title = "Selectionner la Photo à inserer";
ofdImage.CheckFileExists = true;
//ofdImage.InitialDirectory = Application.StartupPath;
ofdImage.Filter = "Fichiers image (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
ofdImage.FilterIndex = 1;
ofdImage.RestoreDirectory = true;

if (ofdImage.ShowDialog() == DialogResult.OK)
{
// Affichage de la photo au format bitmap
this.pbxPhoto.Image = (Image)new Bitmap(ofdImage.FileName);
this.txtNomPhoto.Text = ofdImage.FileName.ToString();

// Pas encore d'id pour la nouvelle Photo
this.gbxEquipage.Text = "Nouvel Equipage";
this.gbxEquipage.Tag = null;
}
}
catch (Exception ex)
{
//MessageBox.Show(string.Concat("Erreur: ", ex.Message));
MessageBox.Show(ex.Message, "Erreur");
}
}

pour charger la photo et comme suit:
                                                                                                                                        // Connexion à la base de données
OleDbConnection Conex = new OleDbConnection(strConex);
//Préparation des paramètres
OleDbParameter Param_CodeEqui = new OleDbParameter("@CodeEqui", OleDbType.VarWChar, 50); //0
OleDbParameter Param_CodeGrd = new OleDbParameter("@CodeGrd", OleDbType.Integer, 7);//1
OleDbParameter Param_NomEq = new OleDbParameter("@NomEq", OleDbType.VarWChar, 50); //2
OleDbParameter Param_PrenomEq = new OleDbParameter("@PrenomEq", OleDbType.VarWChar, 50); //3
OleDbParameter Param_CodeSexe = new OleDbParameter("@CodeSexe", OleDbType.Integer, 7);//4
OleDbParameter Param_DateNaiss = new OleDbParameter("@DateNaiss", OleDbType.Date, 8);//5
OleDbParameter Param_CodeLieu = new OleDbParameter("@CodeLieu", OleDbType.Integer, 7);//6
OleDbParameter Param_Pere = new OleDbParameter("@NomPere", OleDbType.VarWChar, 50);//7
OleDbParameter Param_Mere = new OleDbParameter("@NomMere", OleDbType.VarWChar, 50);//8
OleDbParameter Param_Rhesus = new OleDbParameter("@CodeRhesus", OleDbType.Integer, 7);//9
OleDbParameter Param_Ethnie = new OleDbParameter("@CodeEth", OleDbType.Integer, 7);//10
OleDbParameter Param_CodeRlg = new OleDbParameter("@CodeRlg", OleDbType.Integer, 7);//11
OleDbParameter Param_CodeSFam = new OleDbParameter("@CodeSFam", OleDbType.Integer, 7);//12
OleDbParameter Param_CodeNiveau = new OleDbParameter("@CodeNiveau", OleDbType.Integer, 7);//13
OleDbParameter Param_CodeDC = new OleDbParameter("@CodeDC", OleDbType.Integer, 7);//14
OleDbParameter Param_DateEntree = new OleDbParameter("@DateEntree", OleDbType.Date, 10);//15
OleDbParameter Param_CodeSpe = new OleDbParameter("@CodeSpe", OleDbType.Integer, 7);//16
OleDbParameter Param_NumCIM = new OleDbParameter("@NumCIM", OleDbType.VarWChar, 50);//17
OleDbParameter Param_CodeUO = new OleDbParameter("@CodeUO", OleDbType.Integer, 7);//18
OleDbParameter Param_CodeFon = new OleDbParameter("@CodeFon", OleDbType.Integer, 7);//19
OleDbParameter Param_DatePromo = new OleDbParameter("@DatePromo", OleDbType.Date, 10);//20
OleDbParameter Param_Classement = new OleDbParameter("@Classement", OleDbType.Integer, 7);//21
OleDbParameter Param_CodeDM = new OleDbParameter("@CodeDM", OleDbType.Integer, 7);//22
OleDbParameter Param_Contact = new OleDbParameter("@Contact", OleDbType.VarWChar, 15); //23
OleDbParameter Param_Photo = new OleDbParameter("@Photo", OleDbType.VarBinary); //24
OleDbParameter Param_Mecano = new OleDbParameter("@Mecano", OleDbType.Integer, 7);//25
OleDbParameter Param_DateFin = new OleDbParameter("@DateFin", OleDbType.Date, 8);//26
Param_CodeEqui.Value = this.txtMatricule.Text; //0
Param_CodeSpe.Value = this.txtCodeSpecialite.Text;//1
Param_Rhesus.Value = this.txtCodeRhesus.Text;//2
Param_CodeDC.Value = this.txtCodeDiplomeCivil.Text;//3
Param_CodeDM.Value = this.txtCodeDiplomeMilitaire.Text;//4
Param_CodeLieu.Value = this.txtCodeLieu.Text;//5
Param_CodeNiveau.Value = this.txtCodeNiveauEtude.Text;//6
Param_CodeGrd.Value = this.txtCodeGrade.Text;//7
Param_CodeRlg.Value = this.txtCodeReligion.Text;//8
Param_CodeFon.Value = this.txtCodeFonction.Text;//9
Param_Ethnie.Value = this.txtCodeEthnie.Text;//10
Param_CodeUO.Value = this.txtCodeUniteOrigine.Text;//11
Param_CodeSexe.Value = this.txtCodeSexe.Text;//12
Param_CodeSFam.Value = this.txtCode_Sit_Famille.Text;//13
Param_NomEq.Value = this.txtNomEquipage.Text;//14
Param_PrenomEq.Value = this.txtPrenomEquipage.Text;//15
Param_DateNaiss.Value = this.txtDateNaissance.Text;//16
Param_Pere.Value = this.txtNomPere.Text;//17
Param_Mere.Value = this.txtNomMere.Text;//18
Param_DateEntree.Value = this.tDateEntree.Text;//19
Param_NumCIM.Value = this.txtNumeroCIM.Text;//20
Param_DatePromo.Value = this.tDatePromotion.Text;//21
Param_Classement.Value = this.txtOrdrePassage.Text;//22
Param_Contact.Value = this.txtTelephone.Text;//23
Param_Photo.Value = this.bytPhoto;//24
Param_Mecano.Value = this.txtMecano.Text;//25
Param_DateFin.Value = this.txtDateRetraite.Text;//26
string sInsertionSQL = string.Format("INSERT INTO PERSONNELS([matricule], [code_specialite], [code_rhesus], [code_diplome_civil]," + "\n" +
"[code_diplome_militaire], [code_lieu], [code_niveau_etude], [code_grade], [code_religion], [code_fonction], [code_ethnie]," + "\n" +
"[code_unite], [code_sexe], [code_situation_famille], [nom], [prenoms], [date_naissance], [nom_pere], [nom_mere]," + "\n" +
"[date_entree], [numero_cim], [date_promotion], [classement], [telephone], [photo], [mecano], [date_fin_lien])" + "\n" +
"VALUES ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22}," + "\n" +
"{23},{24},{25},{26})",
Param_CodeEqui.ParameterName, //0
Param_CodeSpe.ParameterName,//1
Param_Rhesus.ParameterName,//2
Param_CodeDC.ParameterName,//3
Param_CodeDM.ParameterName,//4
Param_CodeLieu.ParameterName,//5
Param_CodeNiveau.ParameterName,//6
Param_CodeGrd.ParameterName,//7
Param_CodeRlg.ParameterName,//8
Param_CodeFon.ParameterName,//9
Param_Ethnie.ParameterName,//10
Param_CodeUO.ParameterName,//11
Param_CodeSexe.ParameterName,//12
Param_CodeSFam.ParameterName,//13
Param_NomEq.ParameterName,//14
Param_PrenomEq.ParameterName,//15
Param_DateNaiss.ParameterName,//16
Param_Pere.ParameterName,//17
Param_Mere.ParameterName,//18
Param_DateEntree.ParameterName,//19
Param_NumCIM.ParameterName,//20
Param_DatePromo.ParameterName,//21
Param_Classement.ParameterName,//22
Param_Contact.ParameterName,//23
Param_Photo.ParameterName,//24
Param_Mecano.ParameterName,//25
Param_DateFin.ParameterName);//26
OleDbCommand Comd = new OleDbCommand(sInsertionSQL.ToString(), Conex);

Comd.Parameters.Add(Param_CodeEqui);//0
Comd.Parameters.Add(Param_CodeSpe);//1
Comd.Parameters.Add(Param_Rhesus);//2
Comd.Parameters.Add(Param_CodeDC);//3
Comd.Parameters.Add(Param_CodeDM);//4
Comd.Parameters.Add(Param_CodeLieu);//5
Comd.Parameters.Add(Param_CodeNiveau);//6
Comd.Parameters.Add(Param_CodeGrd);//7
Comd.Parameters.Add(Param_CodeRlg);//8
Comd.Parameters.Add(Param_CodeFon);//9
Comd.Parameters.Add(Param_Ethnie);//10
Comd.Parameters.Add(Param_CodeUO);//11
Comd.Parameters.Add(Param_CodeSexe);//12
Comd.Parameters.Add(Param_CodeSFam);//13
Comd.Parameters.Add(Param_NomEq);//14
Comd.Parameters.Add(Param_PrenomEq);//15
Comd.Parameters.Add(Param_DateNaiss);//16
Comd.Parameters.Add(Param_Pere);//17
Comd.Parameters.Add(Param_Mere);//18
Comd.Parameters.Add(Param_DateEntree);//19
Comd.Parameters.Add(Param_NumCIM);//20
Comd.Parameters.Add(Param_DatePromo);//21
Comd.Parameters.Add(Param_Classement);//22
Comd.Parameters.Add(Param_Contact);//23
Comd.Parameters.Add(Param_Photo);//24
Comd.Parameters.Add(Param_Mecano);//25
Comd.Parameters.Add(Param_DateFin);//26
Conex.Open();
Comd.ExecuteNonQuery();

MessageBox.Show("Nouvel Enregistrement\najouté avec succès", "Ajout", MessageBoxButtons.OK, MessageBoxIcon.Information);

Conex.Close();

pour l'enregistrement.
Maintenant je souhaiterais enregistrer le chemin de la photo.
Merci de m'aider.

1 réponse

Messages postés
14770
Date d'inscription
vendredi 14 mars 2003
Statut
Modérateur
Dernière intervention
27 février 2021
151
Le chemin du fichier est un simple champ texte, non ?