GestionDeDommerceInformatiq.../GestionDeCommerceInfoClasse.../DataLayer/BDDAccesArticle.cs

159 lines
7.2 KiB
C#
Raw Permalink Normal View History

2018-06-07 17:40:51 +02:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GestionDeCommerceInfoClasseBDDNCouches.DataLayer
{
public class BDDAccesArticle : BDDAccesBase
{
//private string sCHConnBDD;
//public string SCHConnBDD { get => sCHConnBDD; set => sCHConnBDD = value; }
//private SqlCommand CommandSQL = new SqlCommand();
public BDDAccesArticle(string schConn) : base(schConn)
{
//SCHConnBDD = schConn;
//CommandSQL.Connection = new SqlConnection(schConn);
}
public List<Article> ListArticles()
{
CommandSQL.Parameters.Clear();
List<Article> lstArt = new List<Article>();
CommandSQL.CommandType = System.Data.CommandType.StoredProcedure;
CommandSQL.CommandText = "ListArticle";
CommandSQL.Parameters.AddWithValue("@ID", Convert.DBNull);
CommandSQL.Connection.Open();
SqlDataReader data = CommandSQL.ExecuteReader();
while (data.Read())
{
//Console.WriteLine(data.ToString()+"\n"+ (data["ID"].ToString()) +"__"+ (data["PrixHTVA"].ToString())+"__"+(data["Stock"].ToString())+"__"+ ((data["Visible"].ToString()))+"__"+ ((data["Actif"].ToString())));
lstArt.Add(new Article(int.Parse(data["ID"].ToString()), data["Designation"].ToString(),
float.Parse(data["PrixHTVA"].ToString()),
int.Parse(data["Stock"].ToString()),
(bool.Parse(data["Visible"].ToString())),
(bool.Parse(data["Actif"].ToString()))));
}
CommandSQL.Connection.Close();
return lstArt;
}
public List<Article> VerifStock(int alerteStock)
{
CommandSQL.Parameters.Clear();
List<Article> lstArt = new List<Article>();
CommandSQL.CommandType = System.Data.CommandType.StoredProcedure;
CommandSQL.CommandText = "StockUnder";
CommandSQL.Parameters.AddWithValue("@Stock", alerteStock);
CommandSQL.Connection.Open();
SqlDataReader data = CommandSQL.ExecuteReader();
while (data.Read())
{
//Console.WriteLine(data.ToString()+"\n"+ (data["ID"].ToString()) +"__"+ (data["PrixHTVA"].ToString())+"__"+(data["Stock"].ToString())+"__"+ ((data["Visible"].ToString()))+"__"+ ((data["Actif"].ToString())));
lstArt.Add(new Article(int.Parse(data["ID"].ToString()), data["Designation"].ToString(),
float.Parse(data["PrixHTVA"].ToString()),
int.Parse(data["Stock"].ToString()),
(bool.Parse(data["Visible"].ToString())),
(bool.Parse(data["Actif"].ToString()))));
}
CommandSQL.Connection.Close();
return lstArt;
}
public Article LireArticle(int id)
{
CommandSQL.Parameters.Clear();
Article lstArt;
CommandSQL.CommandType = System.Data.CommandType.StoredProcedure;
CommandSQL.CommandText = "ListArticle";
CommandSQL.Parameters.AddWithValue("@ID", id);
CommandSQL.Connection.Open();
SqlDataReader data = CommandSQL.ExecuteReader();
if (data.Read())
{
//Console.WriteLine(data.ToString()+"\n"+ (data["ID"].ToString()) +"__"+ (data["PrixHTVA"].ToString())+"__"+(data["Stock"].ToString())+"__"+ ((data["Visible"].ToString()))+"__"+ ((data["Actif"].ToString())));
lstArt = new Article(int.Parse(data["ID"].ToString()), data["Designation"].ToString(),
float.Parse(data["PrixHTVA"].ToString()),
int.Parse(data["Stock"].ToString()),
(bool.Parse(data["Visible"].ToString())),
(bool.Parse(data["Actif"].ToString())));
CommandSQL.Connection.Close();
return lstArt;
}
else
{
CommandSQL.Connection.Close();
return null;
}
}
public int AjouterArticle(Article article)
{
CommandSQL.CommandType = System.Data.CommandType.StoredProcedure;
CommandSQL.CommandText = "AjoutArticle";
CommandSQL.Parameters.Clear();
CommandSQL.Parameters.Add("ID", SqlDbType.Int);
Direction("ID", ParameterDirection.Output);
CommandSQL.Parameters.AddWithValue("@Designation",article.Designation);
CommandSQL.Parameters.AddWithValue("@PrixHTVA",article.PrixHTVA);
CommandSQL.Parameters.AddWithValue("@Stock",article.Stock);
CommandSQL.Parameters.AddWithValue("@Visible",article.Visible);
CommandSQL.Parameters.AddWithValue("@Actif",article.Actif);
CommandSQL.Connection.Open();
SqlDataReader data = CommandSQL.ExecuteReader();
int a = int.Parse(CommandSQL.Parameters["ID"].Value.ToString());
article.ID = a;
CommandSQL.Connection.Close();
return a;
}
public void AjouterArticles(List<Article> articles)
{
foreach (Article article in articles)
article.ID = AjouterArticle(article);
}
public void ModifArticle(Article article)
{
CommandSQL.Parameters.Clear();
CommandSQL.CommandType = System.Data.CommandType.StoredProcedure;
CommandSQL.CommandText = "ModifArticle";
CommandSQL.Parameters.AddWithValue("@ID", article.ID);
CommandSQL.Parameters.AddWithValue("@Designation", article.Designation);
CommandSQL.Parameters.AddWithValue("@PrixHTVA", article.PrixHTVA);
CommandSQL.Parameters.AddWithValue("@Stock", article.Stock);
CommandSQL.Parameters.AddWithValue("@Visible", article.Visible);
CommandSQL.Parameters.AddWithValue("@Actif", article.Actif);
CommandSQL.Connection.Open();
SqlDataReader data = CommandSQL.ExecuteReader();
CommandSQL.Connection.Close();
}
public void ModifArticles(List<Article> articles)
{
foreach (Article article in articles)
ModifArticle(article);
}
public void SuprimerArticle(int id, bool definitivement)
{
CommandSQL.Parameters.Clear();
CommandSQL.CommandType = System.Data.CommandType.StoredProcedure;
CommandSQL.CommandText = "SuprimerArticle";
CommandSQL.Parameters.AddWithValue("@ID", id);
CommandSQL.Parameters.AddWithValue("@definitivement", definitivement);
CommandSQL.Connection.Open();
SqlDataReader data = CommandSQL.ExecuteReader();
CommandSQL.Connection.Close();
}
public void SuprimerArticle(Article article, bool definitivement)
{
SuprimerArticle(article.ID, definitivement);
}
public void SuprimerArticle(List<Article> articles, bool definitivement)
{
foreach (Article article in articles)
SuprimerArticle(article.ID, definitivement);
}
}
}