ProjetThe/ProjetTheAlone/Model/A_T_plat.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2019-01-18 13:08:55 +01:00
#region Ressources extérieures
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using ProjetTheAlone.Classes;
#endregion
namespace ProjetTheAlone.Acces
{
/// <summary>
/// Couche d'accès aux données (Data Access Layer)
/// </summary>
public class A_T_plat : ADBase
{
#region Constructeurs
public A_T_plat(string sChaineConnexion)
: base(sChaineConnexion)
{ }
#endregion
public int Ajouter(string P_nom, byte[] P_img, int? ID_typePlat)
{
CreerCommande("AjouterT_plat");
int res = 0;
Commande.Parameters.Add("ID_plat", SqlDbType.Int);
Direction("ID_plat", ParameterDirection.Output);
if(P_nom == null) Commande.Parameters.AddWithValue("@P_nom", Convert.DBNull);
else Commande.Parameters.AddWithValue("@P_nom", P_nom);
if(P_img == null) Commande.Parameters.AddWithValue("@P_img", Convert.DBNull);
else Commande.Parameters.AddWithValue("@P_img", P_img);
if(ID_typePlat == null) Commande.Parameters.AddWithValue("@ID_typePlat", Convert.DBNull);
else Commande.Parameters.AddWithValue("@ID_typePlat", ID_typePlat);
Commande.Connection.Open();
Commande.ExecuteNonQuery();
res = int.Parse(LireParametre("ID_plat"));
Commande.Connection.Close();
return res;
}
public int Modifier(int ID_plat, string P_nom, byte[] P_img, int? ID_typePlat)
{
CreerCommande("ModifierT_plat");
int res = 0;
Commande.Parameters.AddWithValue("@ID_plat", ID_plat);
if(P_nom == null) Commande.Parameters.AddWithValue("@P_nom", Convert.DBNull);
else Commande.Parameters.AddWithValue("@P_nom", P_nom);
if(P_img == null) Commande.Parameters.AddWithValue("@P_img", Convert.DBNull);
else Commande.Parameters.AddWithValue("@P_img", P_img);
if(ID_typePlat == null) Commande.Parameters.AddWithValue("@ID_typePlat", Convert.DBNull);
else Commande.Parameters.AddWithValue("@ID_typePlat", ID_typePlat);
Commande.Connection.Open();
Commande.ExecuteNonQuery();
Commande.Connection.Close();
return res;
}
public List<C_T_plat> Lire(string Index)
{
CreerCommande("SelectionnerT_plat");
Commande.Parameters.AddWithValue("@Index", Index);
Commande.Connection.Open();
SqlDataReader dr = Commande.ExecuteReader();
List<C_T_plat> res = new List<C_T_plat>();
while (dr.Read())
{
C_T_plat tmp = new C_T_plat();
tmp.ID_plat = int.Parse(dr["ID_plat"].ToString());
if(dr["P_nom"] != DBNull.Value) tmp.P_nom = (dr["P_nom"].ToString());
if(dr["P_img"] != DBNull.Value) tmp.P_img = Encoding.ASCII.GetBytes(dr["P_img"].ToString());
if(dr["ID_typePlat"] != DBNull.Value) tmp.ID_typePlat = int.Parse(dr["ID_typePlat"].ToString());
res.Add(tmp);
}
dr.Close();
Commande.Connection.Close();
return res;
}
public C_T_plat Lire_ID(int ID_plat)
{
CreerCommande("SelectionnerT_plat_ID");
Commande.Parameters.AddWithValue("@ID_plat", ID_plat);
Commande.Connection.Open();
SqlDataReader dr = Commande.ExecuteReader();
C_T_plat res = new C_T_plat();
while (dr.Read())
{
res.ID_plat = int.Parse(dr["ID_plat"].ToString());
if(dr["P_nom"] != DBNull.Value) res.P_nom = (dr["P_nom"].ToString());
if(dr["P_img"] != DBNull.Value) res.P_img = Encoding.ASCII.GetBytes(dr["P_img"].ToString());
if(dr["ID_typePlat"] != DBNull.Value) res.ID_typePlat = int.Parse(dr["ID_typePlat"].ToString());
}
dr.Close();
Commande.Connection.Close();
return res;
}
public int Supprimer(int ID_plat)
{
CreerCommande("SupprimerT_plat");
int res = 0;
Commande.Parameters.AddWithValue("@ID_plat", ID_plat);
Commande.Connection.Open();
res = Commande.ExecuteNonQuery();
Commande.Connection.Close();
return res;
}
}
}