0% found this document useful (0 votes)
4 views

code C#

The document is a C# class named ClsGlossiaire that provides methods for database operations such as connecting to a SQL database, executing CRUD operations, and generating reports. It includes functionalities for managing users, categories, packages, and expeditions, along with error handling and user feedback through message boxes. The class follows a singleton pattern for instance management and utilizes ADO.NET for database interactions.

Uploaded by

Guill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

code C#

The document is a C# class named ClsGlossiaire that provides methods for database operations such as connecting to a SQL database, executing CRUD operations, and generating reports. It includes functionalities for managing users, categories, packages, and expeditions, along with error handling and user feedback through message boxes. The class follows a singleton pattern for instance management and utilizes ADO.NET for database interactions.

Uploaded by

Guill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace GestionConge.Classes
{
class ClsGlossiaire
{
public static ClsGlossiaire _instance = null;
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataAdapter dt = null;
SqlDataReader dr = null;
Clsconnexion cnx = null;
DataSet ds = null;

public static ClsGlossiaire Getinstance()


{
if (_instance == null)
_instance = new ClsGlossiaire();
return _instance;
}

public void innitialiseconnect()


{
try
{
cnx = new Clsconnexion();
cnx.connect();
con = new SqlConnection(cnx.chemin);
}
catch (Exception e)
{
MessageBox.Show("L'un de vos fichiers de configuration est
incorrect !" + e.Message);
}
}

public void Appel(Panel pan, Control ctrl)


{
pan.Controls.Clear();
pan.Controls.Add(ctrl);
pan.Show();
}

public void Supprimer(string nomTable, string nomChamp, int id)


{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("DELETE FROM " + nomTable + " WHERE " +
nomChamp + "=@Id", con);
cmd.Parameters.AddWithValue("@Id", id);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
cmd.Dispose();
}
}
public DataTable GetTable(string nomTable)
{
innitialiseconnect();
if (!con.State.ToString().Trim().ToLower().Equals("open")) ;
con.Open();
cmd = new SqlCommand("SELECT * FROM " + nomTable + "", con);
dt = null;
dt = new SqlDataAdapter(cmd);
ds = new DataSet();

dt.Fill(ds);
con.Close();
return ds.Tables[0];
}
public void chargementComboBox(ComboBox cmb, string nomChamp, string
nomTable)
{
innitialiseconnect();
if (!con.State.ToString().Trim().ToLower().Equals("open")) con.Open();
using (IDbCommand cmd = con.CreateCommand())
{
cmd.CommandText = @"select distinct " + nomChamp + " from " +
nomTable + "";

IDataReader rd = cmd.ExecuteReader();

while (rd.Read())
{
string de = rd[nomChamp].ToString();
cmb.Items.Add(de);
}
rd.Close();
rd.Dispose();
cmd.Dispose();
}
}
public string GetID(String champ, String table, String champcondition1,
String valeur1)
{
string _id = string.Empty;

innitialiseconnect();
if (!con.State.ToString().Trim().ToLower().Equals("open")) con.Open();
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT DISTINCT " + champ + " FROM " + table + "
WHERE " + champcondition1 + " = @valeur1";
cmd.Parameters.Add(new SqlParameter("@valeur1",
SqlDbType.NVarChar)).Value = valeur1;
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.HasRows)
while (dr.Read())
_id = dr.GetFieldValue<object>(0).ToString();
cmd.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
return _id.ToString();
}
private static void setParameter(SqlCommand cmd, string name, DbType type,
int length, object paramValue)
{
IDbDataParameter a = cmd.CreateParameter();
a.ParameterName = name;
a.Size = length;
a.DbType = type;

if (paramValue == null)
{
if (!a.IsNullable)
{
a.DbType = DbType.String;
}
a.Value = DBNull.Value;
}
else
a.Value = paramValue;
cmd.Parameters.Add(a);
}
public DataTable recherche_Infromation(string NomTable, string Nom, string
Postnom, string Prenom, string recherche)
{
innitialiseconnect();
if (!con.State.ToString().ToLower().Equals("open")) con.Open();
cmd = new SqlCommand("select * from " + NomTable + " WHERE " + Nom + "
LIKE '%" + recherche + "%' or " + Postnom + " LIKE '%" + recherche + "%' or " +
Prenom + " LIKE '%" + recherche + "%' ", con);
dt = null;
dt = new SqlDataAdapter(cmd);
ds = new DataSet();
dt.Fill(ds);
con.Close();
return ds.Tables[0];
}
public DataSet get_Report_X(string nomTable, string nomchamp, int valchamp)
{
DataSet dst;
try
{
innitialiseconnect();
if (!con.State.ToString().ToLower().Equals("open")) con.Open();
cmd = new SqlCommand("SELECT * FROM " + nomTable + " WHERE " +
nomchamp + "=@valchamp", con);
cmd.Parameters.AddWithValue("@valchamp", valchamp);
dt = new SqlDataAdapter(cmd);
dst = new DataSet();
dt.Fill(dst, nomTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
dt.Dispose(); con.Close();
}
return dst;
}

public DataSet get_Report_Trier(string nomTable, string nomchamp, DateTime


val1, DateTime val2)
{
DataSet dst;
try
{
innitialiseconnect();
if (!con.State.ToString().ToLower().Equals("open")) con.Open();
cmd = new SqlCommand("SELECT * FROM " + nomTable + " WHERE " +
nomchamp + " between @date1 and @date2 ", con);
setParameter(cmd, "@date1", DbType.DateTime, 30, val1);
setParameter(cmd, "@date2", DbType.DateTime, 30, val2);
dt = new SqlDataAdapter(cmd);
dst = new DataSet();
dt.Fill(dst, nomTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
dt.Dispose(); con.Close();
}
return dst;
}

public DataSet get_Report_S(string nomTable, string idTable)


{
DataSet dst;
try
{
innitialiseconnect();
if (!con.State.ToString().ToLower().Equals("open")) con.Open();
cmd = new SqlCommand("SELECT * FROM " + nomTable + " ORDER BY " +
idTable + "", con);
dt = new SqlDataAdapter(cmd);
dst = new DataSet();
dt.Fill(dst, nomTable);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
dt.Dispose(); con.Close();
}
return dst;
}
public void backup(string bd, string disk)
{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("backup database " + bd + " to disk='" + disk
+ "gestion_colis_bd.bak'", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Sauvegarde reussi avec succes !!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}
public DataTable ChargerTable(String table)
{
DataTable dTable = new DataTable();

try
{
innitialiseconnect();
if (!con.State.ToString().Trim().ToLower().Equals("open"))
con.Open();

SqlCommand cmd = con.CreateCommand();

cmd.CommandText = "SELECT * FROM " + table + "";


SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dTable);
}
catch (Exception exc)
{
throw new Exception(exc.Message);
}
finally
{
con.Close();
}
return dTable;
}
public DataTable ChargerTable(String table, String champcondition, String
valeur)
{
DataTable dTable = new DataTable();

try
{
innitialiseconnect();
if (!con.State.ToString().Trim().ToLower().Equals("open"))
con.Open();

SqlCommand cmd = con.CreateCommand();

cmd.CommandText = "SELECT * FROM " + table + " WHERE " +


champcondition + " = @champcondition";

cmd.Parameters.Add(new SqlParameter("@champcondition",
SqlDbType.NVarChar)).Value = valeur;

SqlDataAdapter adapter = new SqlDataAdapter(cmd);


adapter.Fill(dTable);
}
catch (Exception exc)
{
throw new Exception(exc.Message);
}
finally
{
con.Close();
}
return dTable;
}
public void insertUser(ClsUser cb)
{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute saveUser @code,@nom,@pass,@niveau",
con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@nom", cb.Nom);
cmd.Parameters.AddWithValue("@pass", cb.Pass);
cmd.Parameters.AddWithValue("@niveau", cb.Niveau);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}
public void insertCategorie(Clscategorie cb)
{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute saveCategorie @code,@nature", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@nature", cb.Nature);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

public void insertColis(ClsColis cb)


{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute saveColis
@code,@designation,@nombre_colis,@codecolis,@refcategorie,@refexpediteur", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@designation", cb.Designation);
cmd.Parameters.AddWithValue("@nombre_colis", cb.Nombrecolis);
cmd.Parameters.AddWithValue("@codecolis", cb.Codecolis);
cmd.Parameters.AddWithValue("@refcategorie", cb.Refcategorie);
cmd.Parameters.AddWithValue("@refexpediteur", cb.Refexpediteur);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

public void insertExpedition(ClsExpedition cb)


{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute saveExpedition
@code,@refcolis,@dateExp,@nombre_colis,@refexpediteur,@refdestinateurr,@destination
", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@refcolis", cb.Refcolis);
setParameter(cmd, "@dateExp", DbType.DateTime, 30, cb.Date);
cmd.Parameters.AddWithValue("@nombre_colis", cb.Nombrecolis);
cmd.Parameters.AddWithValue("@refexpediteur", cb.Refexpediteur);
cmd.Parameters.AddWithValue("@refdestinateurr", cb.Refdestinateur);
cmd.Parameters.AddWithValue("@destination", cb.Destination);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

public void insertPeser(ClsPeser cb)


{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute savePeser
@code,@refcolis,@poids,@nombre_colis,@datepese,@refpointeur", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@refcolis", cb.Refcolis);
cmd.Parameters.AddWithValue("@poids", cb.Poids);
cmd.Parameters.AddWithValue("@nombre_colis", cb.Nombrecolis);
setParameter(cmd, "@datepese", DbType.DateTime, 30, cb.Date);
cmd.Parameters.AddWithValue("@refpointeur", cb.Refpointeur);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

public void insertExpediteur(ClsExpediteur cb)


{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute saveExpediteur
@code,@nom,@postnom,@prenom,@sexe,@adresse,@telephone", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@nom", cb.Nom);
cmd.Parameters.AddWithValue("@postnom", cb.Postnom);
cmd.Parameters.AddWithValue("@prenom", cb.Prenom);
cmd.Parameters.AddWithValue("@sexe", cb.Sexe);
cmd.Parameters.AddWithValue("@adresse", cb.Adresse);
cmd.Parameters.AddWithValue("@telephone", cb.Telephone);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}
public void insertdestinateur(Clsdestinateur cb)
{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute savedestinateur
@code,@nom,@postnom,@prenom,@sexe,@adresse,@telephone", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@nom", cb.Nom);
cmd.Parameters.AddWithValue("@postnom", cb.Postnom);
cmd.Parameters.AddWithValue("@prenom", cb.Prenom);
cmd.Parameters.AddWithValue("@sexe", cb.Sexe);
cmd.Parameters.AddWithValue("@adresse", cb.Adresse);
cmd.Parameters.AddWithValue("@telephone", cb.Telephone);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}
public void insertPointeur(ClsPointeur cb)
{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("execute savepointeur
@code,@nom,@postnom,@prenom,@sexe,@adresse,@telephone", con);
cmd.Parameters.AddWithValue("@code", cb.Id);
cmd.Parameters.AddWithValue("@nom", cb.Nom);
cmd.Parameters.AddWithValue("@postnom", cb.Postnom);
cmd.Parameters.AddWithValue("@prenom", cb.Prenom);
cmd.Parameters.AddWithValue("@sexe", cb.Sexe);
cmd.Parameters.AddWithValue("@adresse", cb.Adresse);
cmd.Parameters.AddWithValue("@telephone", cb.Telephone);
cmd.ExecuteNonQuery();
MessageBox.Show("Enregistrement avec succes!");
}
catch (Exception ex)
{
MessageBox.Show("Impossible de faire l'Operation" + ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

public void testLogin(string nom, string pass)


{
try
{
innitialiseconnect();
con.Open();
cmd = new SqlCommand("SELECT nom, pass,niveau from utilisateur
where nom=@nom and pass=@pass", con);
cmd.Parameters.AddWithValue("@nom", nom);
cmd.Parameters.AddWithValue("@pass", pass);

SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
frmHome p = new frmHome();
p.Show();
}
else
{
MessageBox.Show("Connexion Echoué");
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}

You might also like