1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ProjetC
{
/// <summary>
/// La classe DataC sert à créer un objet de gestion des donnée.
/// Il prend en paramettre un indentifiant de chaine de connexion placée dans le web.config.
/// Il contient des méthodes pour accéder facilement aux données depuis le code.
/// Il gère tout le montage de la connexion et gère ses erreur avec la classe AppExecption, plus bas dans le fichier
/// </summary>
public class DataC
{
/// <summary>
/// Déclaration des propriétés de la classe.
/// Il faut une chaine de connexion, une DataTable et un Adapteur
/// </summary>
private static SqlConnection _conex;
private static DataTable _table;
private static SqlDataAdapter _adapteur;
public static SqlConnection conex
{
get
{
return _conex;
}
set
{
_conex = value;
}
}
public static System.Data.DataTable table
{
get
{
return _table;
}
set
{
_table = value;
}
}
public static SqlDataAdapter adapteur
{
get
{
return _adapteur;
}
set
{
_adapteur = value;
}
}
/// <summary>
/// Création de l'objet de connexion lui même
/// </summary>
/// <param name="idChainCo">Identifiant de la chaine de connexion dans le web.config.</param>
public DataC(string idChainCo)
{
string sRQ = ConfigurationManager.ConnectionStrings[idChainCo].ConnectionString;
if (sRQ == "")
{
//throw new AppExecption("Chaine de connexion " + idChainCo + " introuvable");
}
conex = new SqlConnection(sRQ);
}
public static DataTable RetournerTable(string requete)
{
table = new DataTable();
try
{
//On assigne une requête à la comamnde
SqlCommand commande = new SqlCommand(requete);
//On lui assigne une connexion
commande.Connection = conex;
adapteur = new SqlDataAdapter(commande);
SqlCommandBuilder oCommandBuilder = new SqlCommandBuilder(adapteur);
conex.Open();
adapteur.Fill(table);
}
catch ( Exception Ex)
{
throw new AppExecption(Ex);
}
finally
{
conex.Close();
}
return table;
}
public static string RetournerScalaire(string requete)
{
string valret = "";
try
{
RetournerTable(requete);
if (table.Rows.Count == 0)
valret = "";
else
{
DataRow dt = table.Rows[0];
valret = table.Rows[0][0].ToString();
}
}
catch
{
}
finally
{
}
return valret;
}
}
/// <summary>
/// Représente une erreur qui s'est produite lors de l'execution de l'application (elle sera loguer dans un fichier log)
/// </summary>
/// <remarks></remarks>
public class AppExecption : System.ApplicationException
{
/// <summary>
/// Génère une exception et la logue
/// </summary>
/// <param name="MsgError">message de l'erreur</param>
/// <remarks></remarks>
AppExecption(string MsgError) : base(MsgError)
{
}
/// <summary>
/// Génère une exception et la logue
/// </summary>
/// <param name="aException">Objet Exception</param>
/// <remarks></remarks>
AppExecption(Exception aException) : base(aException.Message, aException)
{
}
}
} |