1. La classe Connexion class Connexion public static string chaine; IDbConnection cnx; IDbCommand cmd; IDataReader dr; private string chainesqlserver = "Data Source=localhost;Initial catalog=reservations; User Id=user;password=111"; private string chainemysql = ""; private string chaineaccess = ""; private string chaineoracle = ""; private void ouvrirconnexion(string type) switch (type) // case "MYSQL": // MySqlConnection cnx; // cnx = new MySqlConnection(chaineMysql); // break; case "ACCESS": //OleDbConnection cnx; cnx = new OleDbConnection(chaineAccess); break; case "SQLSERVER": //SqlConnection cnx; cnx = new SqlConnection(chaineSqlServer); break; cnx.open(); public Connexion() //Obtenir le type de bases de données //ConfigurationException ouvrirconnexion("sqlserver"); public Connexion(string type) ouvrirconnexion(type);
public void Fermer() cnx.close(); public DataTable requete(string sql) return dt; cmd = cnx.createcommand(); cmd.commandtext = sql; dr = cmd.executereader(); DataTable dt = new DataTable(); dt.load(dr); dr.close(); public bool Update(string sql) int r; cmd = cnx.createcommand(); cmd.commandtext = sql; r=cmd.executenonquery(); if (r > 0) return true; else return false; 2. La classe CtrlListeHotels public class CtrlListeHotels public List<Ville> GetListeVilles() Connexion cnx=new Connexion(); DataTable dt= cnx.requete("select * from ville");
List<Ville> villes=new List<Ville>(); Ville ville; foreach (DataRow ligne in dt.rows ) ville= new Ville(); ville.nom = ligne["nom"].tostring(); villes.add(ville); cnx.fermer(); return villes; public List<Hotel> GetListeHotels(String ville) Connexion cnx = new Connexion(); +"'"); DataTable dt = cnx.requete("select * from hotel where nomville='" + ville List<Hotel> hotels = new List<Hotel>(); Hotel hotel; foreach (DataRow ligne in dt.rows) hotel = new Hotel(); hotel.nom = ligne["nom"].tostring(); hotel.adresse = ligne["adresse"].tostring(); hotel.prixchambre =(decimal) ligne["prixchambre"] ; hotels.add(hotel); cnx.fermer(); return hotels; public Hotel GetInfoHotel(String nom) Connexion cnx = new Connexion(); DataTable dt = cnx.requete("select * from hotel where nom='" + nom + "'"); Hotel hotel = new Hotel(); hotel.nom = dt.rows[0]["nom"].tostring(); hotel.adresse = dt.rows[0]["adresse"].tostring(); hotel.prixchambre = (decimal)dt.rows[0]["prixchambre"]; return hotel;
3. La classe CtrlReservation public class CtrlReservation public void AjouterReservation(Reservation r) Connexion cnx = new Connexion(); string sql = "insert into reservation values(" + r.numero + ",'" + r.nomhotel + "','" + r.datededebut + "'," + r.nombredejours + "," + r.nombredechambres + ",'" + r.nom + "','" + r.nom + "','" + r.email + "')"; cnx.update(sql); cnx.fermer(); 4. Formulaire Sélection Hôtel public static string nom; public static string adresse; public static string prix; CtrlListeHotels ctr = new CtrlListeHotels(); public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) combobox1.datasource = ctr.getlistevilles(); combobox1.displaymember = "nom"; combobox1.valuemember = "nom"; private void combobox1_selectedindexchanged(object sender, EventArgs e) clear(); lsthotels.datasource = ctr.getlistehotels(combobox1.text); lsthotels.displaymember = "nom"; lsthotels.valuemember = "nom"; private void listbox1_selectedindexchanged(object sender, EventArgs e)
Hotel H = ctr.getinfohotel(lsthotels.text); txtnom.text = lsthotels.text; txtadresse.text = H.Adresse; txtprix.text = H.PrixChambre.ToString(); txtprixeuro.text = (H.PrixChambre / 10).ToString(); private void clear() foreach (Control c in this.controls) if (c is TextBox) c.text = String.Empty; txtprix.clear(); txtprixeuro.clear(); private void button1_click(object sender, EventArgs e) nom=txtnom.text ; adresse=txtadresse.text; prix=txtprix.text; Form2 forme = new Form2(); frome.show() ; 5. Formulaire Réservation public Form2() InitializeComponent(); private void Form2_Load(object sender, EventArgs e) textbox1.text = Form1.nom; textbox2.text = Form1.adresse; textbox3.text = Form1.prix; Random rd = new Random(); textbox7.text = rd.next(999999999).tostring(); private void button1_click(object sender, EventArgs e) Reservation res = new Reservation(); res.numero = int.parse(textbox7.text); res.nomhotel=textbox1.text; res.datededebut =datetimepicker1.value; res.nombredechambres = int.parse(textbox9.text); res.nombredejours = int.parse(textbox10.text); res.montant = decimal.parse(textbox3.text) * decimal.parse(textbox9.text) * decimal.parse(textbox10.text); res.nom = textbox4.text; res.email = textbox5.text; res.adresse = textbox6.text; CtrlReservation ctr = new CtrlReservation(); //appel de la fonction ctr.ajouterreservation(res);
MessageBox.Show("reservation validée, montant= " + res.montant.tostring());