using System;
namespace Couches
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Création de l'objet métier \"Produit\"." );
Produit p =
new Produit
();
Console.WriteLine("Chargement des produits dont le nom commence par \"Produit\"." );
p.LoadProducts("Produit%" );
p.DisplayProducts();
Console.WriteLine("Chargement du produit \"P1\" et renommage." );
p.LoadProduct("P1" );
p.UpdateProductName("Produit 1 renommé" );
p.DisplayProducts();
Console.WriteLine("Chargement de tous les produits et tentative de mise à jour abusive." );
p.LoadProducts(string.Empty);
p.UpdateProductName("Test..." );
p.DisplayProducts();
Console.WriteLine("Chargement de tous les produits dont le nom est \"Produit 2\"" );
Console.WriteLine("et mise à jour (si un seul produit)." );
p.LoadProducts("Produit 2" );
p.UpdateProductName("Test..." );
p.DisplayProducts();
Console.ReadKey();
}
}
}
using System;
using System.Data;
namespace Couches
{
class Produit : DataBase
{
private DataTable dt;
public Produit()
{
// Le constructeur de "DataBase" est implicitement appelé
}
public bool LoadProduct(string productcode)
{
if (Open())
{
object[,
] filters =
new object[1,
2];
filters[0,0] = "code";
filters[0,1] = productcode;
dt = LoadTable("produit", filters);
if (dt != null && dt.Rows.Count == 1)
return true;
}
return false;
}
public bool UpdateProductName(string productname)
{
if (Open() && dt != null && dt.Rows.Count == 1)
{
dt.Rows[0]["nom"] = productname;
if (SaveTable(dt) == 1)
return true;
}
return false;
}
public int LoadProducts(string namefilter)
{
if (!Open())
{
return -1;
}
object[,] filters;
if (namefilter.Length > 0)
{
filters =
new object[1,
2];
filters[0, 0] = "nom";
filters[0, 1] = namefilter;
dt = LoadTable("produit", filters);
}
else
{
filters =
new object[0,
0];
}
dt = LoadTable("produit", filters);
if (dt != null)
{
return dt.Rows.Count;
}
else
{
return -1;
}
}
public void DisplayProducts()
{
Console.WriteLine("--------------------------------------" );
if (dt != null)
{
for (int i = 0, cpt = dt.Rows.Count; i < cpt; i++)
{
Console.WriteLine("Code : {0}", dt.Rows[i]["code"] as string);
Console.WriteLine("Nom : {0}", dt.Rows[i]["nom"] as string);
}
}
else
{
Console.WriteLine("Aucun produit chargé..." );
}
Console.WriteLine("--------------------------------------" );
Console.WriteLine();
}
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace Couches
{
class DataBase
{
private const string ConnectionString = "server=.;database=test;Integrated Security=SSPI";
private SqlConnection cnx;
private SqlDataAdapter da;
protected DataBase()
{
cnx =
new SqlConnection
(ConnectionString
);
}
protected bool Open()
{
if ((cnx.State == ConnectionState.Open))
{
return true;
}
try
{
cnx.Open();
}
catch
{
return false;
}
if (cnx.State == ConnectionState.Open)
{
return true;
}
else
{
return false;
}
}
protected void Close()
{
try
{
cnx.Close();
}
catch { }
}
protected DataTable LoadTable(string TableName, object[,] Filters)
{
if (!(cnx.State == ConnectionState.Open))
{
return null;
}
StringBuilder sb =
new StringBuilder
(string.
Format("select * from {0} where null is null", TableName
));
SqlCommand cmd = cnx.CreateCommand();
cmd.CommandType = CommandType.Text;
for (int i = 0, cpt = Filters.GetLength(0); i < cpt; i++)
{
if (Filters[i, 1].GetType().Name == "String" && (Filters[i, 1] as string).IndexOf('%') > -1)
{
sb.Append(string.Format(" and {0} like @{0}", Filters[i, 0] as string));
}
else
{
sb.Append(string.Format(" and {0} = @{0}", Filters[i, 0] as string));
}
SqlParameter p = cmd.CreateParameter();
p.Direction = ParameterDirection.Input;
p.ParameterName = Filters[i, 0] as string;
p.SqlValue = Filters[i, 1];
cmd.Parameters.Add(p);
}
cmd.CommandText = sb.ToString();
da =
new SqlDataAdapter
(cmd
);
SqlCommandBuilder cb =
new SqlCommandBuilder
(da
);
da.InsertCommand = cb.GetInsertCommand();
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();
DataTable dt =
new DataTable
();
da.Fill(dt);
return dt;
}
protected int SaveTable(DataTable dt)
{
if (!(cnx.State == ConnectionState.Open))
{
return -1;
}
DataTable Changes = dt.GetChanges();
int NbChanges = 0;
if (dt != null && dt.Rows.Count > 0)
{
NbChanges = da.Update(Changes);
dt.AcceptChanges();
}
return NbChanges;
}
}
}