IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Classe connection base de données (Oracle)


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Août 2009
    Messages
    273
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 273
    Par défaut Classe connection base de données (Oracle)
    Bonjour,

    Je cherche un exemple de code basé sur la programmation objet utilisant une classe connexion pouvant être utilisée pour se connecter à une base de données Oracle et SqlServer.

    J'ai trouvé le code ci-dessous mais je ne sais pas s'il existe une meilleure façon de faire?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
     
    using System;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using Oracle.DataAccess.Client;
    using Oracle.DataAccess.Types;
    using System.Collections;
     
    namespace MonProjet
    {
        /// <summary>
        /// Classe permettant l'interface avec Oracle
        /// </summary>
        public class classOracle
        {
            #region Variables
            /// <summary>
            /// Serveur Oracle
            /// </summary>
            private string sHost;
            /// <summary>
            /// Port Oracle
            /// </summary>
            private int sPort;
            /// <summary>
            /// Base Oracle
            /// </summary>
            private string sService_name;
            /// <summary>
            /// User Oracle
            /// </summary>
            private string sUser;
            /// <summary>
            /// Mot de passe Oracle
            /// </summary>
            private string sPassword;
            /// <summary>
            /// Nombre de lignes du résultat d'une requête
            /// </summary>
            private int iNbRows;
            /// <summary>
            /// Nombre de colonnes du résultat d'une requête
            /// </summary>
            private int iNbCols;
            /// <summary>
            /// Connexion à la base de données Oracle
            /// </summary>
            private OracleConnection oracleCnx;
            /// <summary>
            /// Instruction SQL ou procédure stockée à exécuter
            /// </summary>
            private OracleCommand oracleCmd;
            /// <summary>
            /// Flux de lignes de données stockant le résultat d'une requête
            /// </summary>
            private OracleDataReader oracleReader;
            #endregion
     
            #region Propriétés
            /// <summary>
            /// Affecte ou récupère le nom du serveur
            /// </summary>
            public string Host
            {
                get
                {
                    return this.sHost;
                }
     
                set
                {
                    this.sHost = value;
                }
            }
     
            /// <summary>
            /// Affecte ou récupère le port
            /// </summary>
            public int Port
            {
                get
                {
                    return this.Port;
                }
     
                set
                {
                    this.sPort = value;
                }
            }
     
            /// <summary>
            /// Affecte ou récupère la base Oracle
            /// </summary>
            public string Service_name
            {
                get
                {
                    return this.sService_name;
                }
     
                set
                {
                    this.sService_name = value;
                }
            }
     
            /// <summary>
            /// Affecte ou récupère le login Oracle
            /// </summary>
            public string User
            {
                get
                {
                    return this.sUser;
                }
     
                set
                {
                    this.sUser = value;
                }
            }
     
            /// <summary>
            /// Affecte ou récupère le mot de passe Oracle
            /// </summary>
            public string Password
            {
                get
                {
                    return this.sPassword;
                }
     
                set
                {
                    this.sPassword = value;
                }
            }
     
            /// <summary>
            /// Récupère le nombre de ligne d'une requête
            /// </summary>
            public int NbRows
            {
                get
                {
                    return this.iNbRows;
                }
            }
     
            /// <summary>
            /// Récupère le nombre de colone d'une requête
            /// </summary>
            public int NbCols
            {
                get
                {
                    return this.iNbCols;
                }
            }
            #endregion
     
            #region Constructeur de la classe classOracle
            /// <summary>Constructeur de la classe</summary>
            public classOracle()
            {
                this.sHost = "";
                this.sPort = 0;
                this.sService_name = "";
                this.sUser = "";
                this.sPassword = "";
     
                this.iNbRows = 0;
                this.iNbCols = 0;
     
                this.oracleCnx = new OracleConnection();
                this.oracleCmd = new OracleCommand();
                this.oracleReader = null;
            }
            #endregion
     
            #region Connexion à la base de données Oracle
            /// <summary>Lance un ordre de connexion à la base Oracle</summary>
            /// <example>
            /// classOracle  monOracle = new classOracle();
            /// monOracle.setUser("Toto");
            /// monOracle.setPassword("mdp");
            /// monOracle.setBase("maBase");
            /// 
            /// if(this.monObjetOracle.connect())
            /// {
            /// ...
            /// }
            /// </example>
            public bool connect()
            {
                try
                {
                    this.oracleCnx.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST="+this.sHost+")(PORT="+this.sPort+")))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME="+this.sService_name+")));User Id="+this.sUser+";Password="+this.sPassword+";";
                    this.iNbCols = 0;
                    this.iNbRows = 0;
     
                    oracleCnx.Open();
     
                    return true;
                }
                catch (Exception)
                {
                    MessageBox.Show("Impossible de se connecter à la base de données", "Erreur Oracle", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return false;
                }
            }
            #endregion
     
            #region Déconnexion de la base de données Oracle
            /// <summary>
            /// Lance un ordre de déconnexion de la base de données Oracle
            /// </summary>
            /// <example>
            /// classOracle  monOracle = new classOracle();
            /// ...
            ///	this.monOracle.deconnect();
            /// </example>
            public bool deconnect()
            {
                try
                {
                    oracleCnx.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Erreur Oracle", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return false;
                }
            }
            #endregion
     
            #region Récupère les valeurs d'une requête
            /// <summary>Récupère les valeurs d'une requête</summary>
            /// <param name="sQuery">Requête à exécuter</param>
            /// <returns>Renvoi les valeurs de la requête</returns>
            /// <example>
            /// classOracle  monOracle = new classOracle();
            /// monOracle.setUser("Toto");
            /// monOracle.setPassword("mdp");
            /// monOracle.setBase("maBase");
            /// 
            /// if(this.monObjetOracle.connect())
            /// {
            ///		string sQuery="SELECT COUNT(*) FROM MaTable";
            ///		object[,] resultat = this.monOracle.getQueryResult(sQuery);
            ///		this.monOracle.deconnect();
            ///		...
            /// }
            /// </example>
            public Object[,] getQueryResult(string sQuery)
            {
                this.oracleCmd = new OracleCommand(sQuery, this.oracleCnx);
                this.oracleReader = null;
                this.oracleReader = this.oracleCmd.ExecuteReader();
     
                Object[,] oArrResult;
                this.iNbRows = 0;
                this.iNbCols = this.oracleReader.FieldCount;
     
                // Initialisation du tableau de retour
                while (this.oracleReader.Read()) { this.iNbRows++; }
                oArrResult = new Object[this.iNbRows, iNbCols];
     
                // Remplissage du tableau de retour
                try
                {
                    this.oracleReader = this.oracleCmd.ExecuteReader();
                    int i = 0;
                    while (this.oracleReader.Read())
                    {
                        for (int j = 0; j < this.iNbCols; j++)
                        {
                            oArrResult[i, j] = oracleReader.GetOracleValue(j);
                        }
                        i++;
                    }
                }
                catch (OracleException oe)
                {
                    this.gestionErreur(oe);
                    return null;
                }
                finally
                {
                    this.oracleReader.Close();
                    this.oracleCmd = null;
                    this.oracleReader = null;
                }
     
                return oArrResult;
            }
            #endregion
     
            #region Lance un ordre SQL de INSERT, UPDATE ou DELETE
            /// <summary>Lance un ordre SQL de INSERT, UPDATE ou DELETE</summary>
            /// <param name="sQuery">Requête à exécuter</param>
            /// <returns>Renvoi true si l'odre SQL s'est déroulé normallement, false sinon</returns>
            /// <example>
            /// classOracle  monOracle = new classOracle();
            /// monOracle.setUser("Toto");
            /// monOracle.setPassword("mdp");
            /// monOracle.setBase("maBase");
            /// 
            /// if(this.monObjetOracle.connect())
            /// {
            ///		string sQuery="DELETE FROM MaTable";
            ///		bool bResultat = this.monOracle.nonQueryOrder(sQuery);
            ///		this.monOracle.deconnect();
            ///		
            ///		if(bResultat)
            ///		{
            ///		  ...  
            ///		}
            /// }
            /// </example>
            public bool nonQueryOrder(string sQuery)
            {
                this.oracleCmd = new OracleCommand(sQuery, this.oracleCnx);
                try
                {
                    this.oracleCmd.ExecuteNonQuery();
                    return true;
                }
                catch (OracleException OracleEx)
                {
                    this.gestionErreur(OracleEx);
                    return false;
                }
                finally
                {
                    this.oracleCmd = null;
                }
            }
            #endregion
     
            #region Lance une procédure PL/SQL
            /// <summary>
            /// Lance une procédure stockée PL/SQL de type procédure
            /// </summary>
            /// <param name="sQuery">Procédure stockée de type procédure à exécuter</param>
            /// <param name="param">Tableau des paramètres</param>
            /// <returns>vrai si la procédure s'est exécutée correctement, faux sinon</returns>
            /// <example>
            /// classOracle  monOracle = new classOracle();
            ///	monOracle.setUser("Toto");
            ///	monOracle.setPassword("mdp");
            ///	monOracle.setBase("maBase");
            ///
            ///	string param1="blabla";
            ///	string param2="blablabla";
            ///
            ///	string sQuery = "SCHEMA.PACKAGE.MA_PROCEDURE";
            ///	ArrayList param = new ArrayList(2);
            ///	/* En admettant que le premier paramètre de ma procédure stockée MA_PROCEDURE, du package PACKAGE, sous le chéma Oracle SCHEMA, se nomme monPremierParametre (idem pour le deuxième paramètre) */
            ///	param.Add(new string[] {"monPremierParametre",param1});
            ///	param.Add(new string[] {"monDeuxièmeParametre",param2});				
            ///
            /// if(this.monOracle.connect())
            /// {
            ///		bool bRetour =  this.monOracle.procStockOrder(sQuery,param);
            ///		this.monOracle.deconnect();
            ///
            ///		if(bRetour)
            ///		{
            ///			...
            ///		}
            ///	}
            /// </example>
            public bool procStockOrder(string sQuery, ArrayList param)
            {
                this.oracleCmd = this.oracleCnx.CreateCommand();
                this.oracleCmd.CommandText = sQuery;
                this.oracleCmd.CommandType = CommandType.StoredProcedure;
     
                foreach (string[] tableau in param)
                {
                    this.oracleCmd.Parameters.Add(tableau[0], tableau[1]);
                }
     
                try
                {
                    this.oracleCmd.ExecuteNonQuery();
                }
                catch (OracleException OracleEx)
                {
                    this.gestionErreur(OracleEx);
                    return false;
                }
                finally
                {
                    this.oracleCmd = null;
                }
     
                return true;
            }
            #endregion
     
            #region Lance une fonction PL/SQL
            /// <summary>
            /// Lance une procédure stockée PL/SQL de type fonction
            /// </summary>
            /// <param name="sQuery">Procédure stockée de type fonction à exécuter</param>
            /// <param name="param">Tableau des paramètres</param>
            /// <param name="sRetour">Intitulé de la valeur de retour</param>
            /// <param name="iLongueurRetour">Longueur de la variable de retour</param>
            /// <returns>la chaîne de caractères retournée par la fonction PL/SQL</returns>
            /// <example>
            /// classOracle  monOracle = new classOracle();
            ///	monOracle.setUser("Toto");
            ///	monOracle.setPassword("mdp");
            ///	monOracle.setBase("maBase");
            ///
            ///	string param1="blabla";
            ///	string param2="blablabla";
            ///
            ///	string sQuery = "SCHEMA.PACKAGE.MA_FONCTION";
            ///	ArrayList param = new ArrayList(2);
            ///	/* En admettant que le premier paramètre de ma fonction stockée MA_FONCTION, du package PACKAGE, sous le chéma Oracle SCHEMA, se nomme monPremierParametre (idem pour le deuxième paramètre) */
            ///	param.Add(new string[] {"monPremierParametre",param1});
            ///	param.Add(new string[] {"monDeuxièmeParametre",param2});				
            ///
            /// if(this.monOracle.connect())
            /// {
            ///		/* En admettant que le paramètre de retour de ma fonction stockée se nomme monParametreRetour et est de longueur 10 (VARCHAR2(10))*/
            ///		string sRetour =  this.monOracle.foncStockOrder(sQuery,param,"monParametreRetour",10);
            ///		this.monOracle.deconnect();
            ///
            ///		if(sRetour != null)
            ///		{
            ///			...
            ///		}
            ///	}
            /// </example>
            public string foncStockOrder(string sQuery, ArrayList param, string sRetour, int iLongueurRetour)
            {
                this.oracleCmd = this.oracleCnx.CreateCommand();
                this.oracleCmd.CommandText = sQuery;
                this.oracleCmd.CommandType = CommandType.StoredProcedure;
                this.oracleCmd.Parameters.Add(sRetour, OracleDbType.NVarchar2, iLongueurRetour);
                this.oracleCmd.Parameters[sRetour].Direction = ParameterDirection.ReturnValue;
     
                foreach (string[] tableau in param)
                {
                    this.oracleCmd.Parameters.Add(tableau[0], tableau[1]);
                }
     
                try
                {
                    this.oracleCmd.ExecuteNonQuery();
                    return ((string)this.oracleCmd.Parameters[sRetour].Value);
                }
                catch (OracleException OracleEx)
                {
                    this.gestionErreur(OracleEx);
                    return null;
                }
                finally
                {
                    this.oracleCmd = null;
                }
            }
            #endregion
     
            #region Gestion des erreurs
            private void gestionErreur(OracleException oe)
            {
                switch (oe.ErrorCode)
                {
                    case 02292:
                        MessageBox.Show("Impossible de supprimer cet élément car il est encore référencé dans une autre table.", "Erreur Oracle", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                        break;
     
                    default:
                        MessageBox.Show(oe.Message, "Erreur Oracle", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                        break;
                }
            }
            #endregion
        }
     
    }
    Je ne maîtrise pas parfaitement la programmation objet. Je me demandais s'il ne fallait pas créer une classe connexion qu'on héritera pour créer la connexion à la base Oracle et la connexion à une base SqlServer?

    Merci,

  2. #2
    Membre Expert Avatar de sisqo60
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Février 2006
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 754
    Par défaut
    Bonjour,

    tu vas avoir des problèmes sur cette propriété!!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public int Port
    {
         get
         {
              return this.Port;//Fait appel au getter de la propriété Port etc....
         }
         set
         {
              this.sPort = value;
         }
    }
    Sinon en termes d'abstraction, toutes les classes XConnection, XDataReader, XCommand etc... où X est généralement ton sqbdr (Oracle, MySql, SqlServer, Firebird, Postgres etc...) on une classe de base DbConnection, DbDataReader, DbCommand etc... Pas besoin de recréer des classes.
    Donc, tu manipules des objets DbQuelquechose pas besoin de créer des classes par toi même. Regardes du coté de DbProviderFactories etc...

    Bon dév.

    EDIT : Tu peux aussi utiliser les interfaces IDbConnection, IDbQuelquechode.

  3. #3
    Membre éclairé
    Inscrit en
    Août 2009
    Messages
    273
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 273
    Par défaut
    Bonjour,

    Si je comprends bien, je n'ai pas besoin de créer une classe connexion. Il me suffit d'utiliser les fonctions existantes d'Oracle comme celà:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     string oradb = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=nom_de_lhote)(PORT=num_port)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=nom _du_service_name)));User Id=nom_user;Password=mot_de_passe;";
     
    OracleConnection conn = new OracleConnection(oradb);
     
    conn.Open();
    Ou bien, devrais-je créer des constructeurs à partir des variables déclarées ci-dessous c'est-à-dire sans prendre en considération les fonctions Oracle comme ci-dessous:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class Connexion
        {
            //Champs
            private string sHost;
            private int iPort;
            private string sService_name;
            private string sUser;
            private string sPassword;
            private int iNbRows;
            private int iNbCols;
     
            //Constructeurs
            public Connexion(){} //Constructeur par défaut
     
            public Connexion(string sHost, int iPort, string sService_name, string sUser, string sPassword)
            {
                this.sHost = sHost;
                this.iPort = iPort;
                this.sService_name = sService_name;
                this.sUser = sUser;
                this.sPassword = sPassword;
            }
     
            public Connexion(Connexion connexion, int iNbRows, int iNbCols)
            {
                this.sHost = connexion.sHost;
                this.iPort = connexion.iPort;
                this.sService_name = connexion.sService_name;
                this.sUser = connexion.sUser;
                this.sPassword = connexion.sPassword;
     
                this.iNbRows = iNbRows;
                this.iNbCols = iNbCols;
            }
    sinon n'est-il pas préférable d'utiliser un fichier de configuration de type App.conf?

    Enfin, vous faites référence au s de Port dans le code ci-dessous?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    public int Port
    {
         get
         {
              return this.Port;//Fait appel au getter de la propriété Port etc....
         }
         set
         {
              this.sPort = value;
         }
    }
    c'est le fait que j'ai écrit

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    public int Port
    {
         get
         {
              return this.Port;//Fait appel au getter de la propriété Port etc....
         }
    }
    au lieu de

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public int Port
    {
         get
         {
              return this.sPort;//Fait appel au getter de la propriété Port etc....
         }
    }
    Merci,

  4. #4
    Membre Expert Avatar de sisqo60
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Février 2006
    Messages
    754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Février 2006
    Messages : 754
    Par défaut
    Citation Envoyé par Jinkas Voir le message
    Si je comprends bien, je n'ai pas besoin de créer une classe connexion. Il me suffit d'utiliser les fonctions existantes d'Oracle comme celà:
    Non tu n'as pas compris, les fonctions du framework .Net. Regardes dans dans le namespace System.Data.Common tu y verra des classes de base pour la manipulation des bases de données. Le lien que je t'ai envoyé lors de mon précédent post, tu l'as lu? (tu sais DbFactories est en bleu et souligné, ça veut dire qu'en cliquant dessus un page internet s'ouvre)

    Click me

  5. #5
    Membre éclairé
    Inscrit en
    Août 2009
    Messages
    273
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 273
    Par défaut
    tu sais DbFactories est en bleu et souligné, ça veut dire qu'en cliquant dessus un page internet s'ouvre
    Merci,

  6. #6
    Membre éclairé
    Inscrit en
    Août 2009
    Messages
    273
    Détails du profil
    Informations forums :
    Inscription : Août 2009
    Messages : 273
    Par défaut
    Bonjour,

    J'utilise la classe suivante basée sur DbProviderFactory pour gérer mes accès aux données:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
     
     public class DbProviderUtilities
        {
     
            private System.Configuration.ConnectionStringsSection _ConnectionStringsSection;
     
            private System.Data.Common.DbProviderFactory _DbProviderFactory;
     
            public System.Data.Common.DbProviderFactory DbProviderFactory
            {
                get { return _DbProviderFactory; }
            }
     
            public System.Configuration.ConnectionStringsSection ConnectionStringsSection
            {
                get { return _ConnectionStringsSection; }
                set { _ConnectionStringsSection = value; }
            }
     
            public DbProviderUtilities()
            { }
     
            /// <summary>
            /// récupère les connections définies dans le fichier de configuration
            /// </summary>
            /// <returns></returns>
            public bool GetConfigurationConnections()
            {
                bool bResult = false;
                try
                {
                    System.Configuration.Configuration oConfiguration = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                    if (oConfiguration.HasFile)
                    {
                        this._ConnectionStringsSection = oConfiguration.ConnectionStrings;
                        bResult = true;
                    }
                    else
                    { }
                }
                catch (System.Configuration.ConfigurationErrorsException ex)
                {
                    throw ex;
                }
     
                return bResult;
     
            }
     
            /// <summary>
            /// crée la connection
            /// </summary>
            /// <returns></returns>
            public System.Data.Common.DbConnection CreateConnection()
            {
                _DbProviderFactory = System.Data.Common.DbProviderFactories.GetFactory(_ConnectionStringsSection.ConnectionStrings[1].ProviderName);
                System.Data.Common.DbConnection oDbConnnection = _DbProviderFactory.CreateConnection();
                oDbConnnection.ConnectionString = _ConnectionStringsSection.ConnectionStrings[1].ConnectionString;
     
                return oDbConnnection;
            }
            /// <summary>
            /// crée la connection 
            /// </summary>
            /// <param name="nConfigurationConnectionIndex"></param>
            /// <returns></returns>
            public System.Data.Common.DbConnection CreateConnection(int nConfigurationConnectionIndex)
            {
                _DbProviderFactory = System.Data.Common.DbProviderFactories.GetFactory(_ConnectionStringsSection.ConnectionStrings[nConfigurationConnectionIndex].ProviderName);
                System.Data.Common.DbConnection oDbConnnection = _DbProviderFactory.CreateConnection();
                oDbConnnection.ConnectionString = _ConnectionStringsSection.ConnectionStrings[nConfigurationConnectionIndex].ConnectionString;
     
                return oDbConnnection;
            }
            /// <summary>
            /// crée la connection
            /// </summary>
            /// <param name="sConfigurationConnectionName"></param>
            /// <returns></returns>
            public System.Data.Common.DbConnection CreateConnection(string sConfigurationConnectionName)
            {
                _DbProviderFactory = System.Data.Common.DbProviderFactories.GetFactory(_ConnectionStringsSection.ConnectionStrings[sConfigurationConnectionName].ProviderName);
                System.Data.Common.DbConnection oDbConnnection = _DbProviderFactory.CreateConnection();
                oDbConnnection.ConnectionString = _ConnectionStringsSection.ConnectionStrings[sConfigurationConnectionName].ConnectionString;
     
                return oDbConnnection;
            }
            /// <summary>
            /// Crée la connection
            /// </summary>
            /// <param name="ProviderName"></param>
            /// <param name="ConnectionString"></param>
            /// <returns></returns>
            public System.Data.Common.DbConnection CreateConnection(string ProviderName, string ConnectionString)
            {
                _DbProviderFactory = System.Data.Common.DbProviderFactories.GetFactory(ProviderName);
                System.Data.Common.DbConnection oDbConnnection = _DbProviderFactory.CreateConnection();
                oDbConnnection.ConnectionString = ConnectionString;
     
                return oDbConnnection;
            }
            /// <summary>
            /// Crée une commande
            /// </summary>
            /// <param name="CommandText"></param>
            /// <param name="oDbConnection"></param>
            /// <param name="CommandType"></param>
            /// <returns></returns>
            public System.Data.Common.DbCommand CreateCommand(String CommandText, System.Data.Common.DbConnection oDbConnection, System.Data.CommandType CommandType)
            {
                System.Data.Common.DbCommand oDbCommand = DbProviderFactory.CreateCommand();
                oDbCommand.CommandType = CommandType;
                oDbCommand.CommandText = CommandText;
                oDbCommand.Connection = oDbConnection;
     
                return oDbCommand;
            }
            public System.Data.Common.DbCommand CreateCommand(String CommandText, System.Data.Common.DbConnection oDbConnection, System.Data.CommandType CommandType, System.Data.Common.DbTransaction Transaction)
            {
                System.Data.Common.DbCommand oDbCommand = DbProviderFactory.CreateCommand();
                oDbCommand.CommandType = CommandType;
                oDbCommand.CommandText = CommandText;
                oDbCommand.Connection = oDbConnection;
                oDbCommand.Transaction = Transaction;
     
                return oDbCommand;
            }
            /// <summary>
            /// crée un paramètre
            /// </summary>
            /// <param name="ParameterName"></param>
            /// <param name="DbType"></param>
            /// <param name="Value"></param>
            /// <returns></returns>
            public System.Data.Common.DbParameter CreateParameter(string ParameterName, System.Data.DbType DbType, object Value)
            {
                System.Data.Common.DbParameter oDbParameter = DbProviderFactory.CreateParameter();
                oDbParameter.ParameterName = ParameterName;
                oDbParameter.DbType = DbType;
                oDbParameter.Value = Value;
     
                return oDbParameter;
            }
            /// <summary>
            /// crée un paramètre
            /// </summary>
            /// <param name="ParameterName"></param>
            /// <param name="DbType"></param>
            /// <param name="ParameterDirection"></param>
            /// <param name="SourceColumn"></param>
            /// <param name="DataRowVersion"></param>
            /// <param name="SourceColumnNullMapping"></param>
            /// <param name="Value"></param>
            /// <returns></returns>
            public System.Data.Common.DbParameter CreateParameter(string ParameterName, System.Data.DbType DbType, System.Data.ParameterDirection ParameterDirection, string SourceColumn, System.Data.DataRowVersion DataRowVersion, bool SourceColumnNullMapping, object Value)
            {
                System.Data.Common.DbParameter oDbParameter = DbProviderFactory.CreateParameter();
                oDbParameter.ParameterName = ParameterName;
                oDbParameter.DbType = DbType;
                oDbParameter.Direction = ParameterDirection;
                oDbParameter.SourceColumn = SourceColumn;
                oDbParameter.SourceVersion = DataRowVersion;
                oDbParameter.SourceColumnNullMapping = SourceColumnNullMapping;
                oDbParameter.Value = Value;
     
                return oDbParameter;
            }
            /// <summary>
            /// crée un paramètre
            /// </summary>
            /// <param name="ParameterName"></param>
            /// <param name="DbType"></param>
            /// <param name="ParameterDirection"></param>
            /// <param name="SourceColumn"></param>
            /// <param name="DataRowVersion"></param>
            /// <param name="Value"></param>
            /// <returns></returns>
            public System.Data.Common.DbParameter CreateParameter(string ParameterName, System.Data.DbType DbType, System.Data.ParameterDirection ParameterDirection, string SourceColumn, System.Data.DataRowVersion DataRowVersion, object Value)
            {
                System.Data.Common.DbParameter oDbParameter = DbProviderFactory.CreateParameter();
                oDbParameter.ParameterName = ParameterName;
                oDbParameter.DbType = DbType;
                oDbParameter.Direction = ParameterDirection;
                oDbParameter.SourceColumn = SourceColumn;
                oDbParameter.SourceVersion = DataRowVersion;
                oDbParameter.Value = Value;
     
                return oDbParameter;
            }
            /// <summary>
            /// crée le mapping d'une table
            /// </summary>
            /// <param name="SourceTable"></param>
            /// <param name="DataSetTable"></param>
            /// <param name="oColumnMappingCollection"></param>
            /// <returns></returns>
            public System.Data.Common.DataTableMapping CreateDataTableMapping(string SourceTable, string DataSetTable, System.Data.Common.DataColumnMappingCollection oDataColumnMappings)
            {
                System.Data.Common.DataTableMapping oDataTableMapping = new System.Data.Common.DataTableMapping(SourceTable, DataSetTable);
                foreach (System.Data.Common.DataColumnMapping oDataColumnMapping in oDataColumnMappings)
                {
                    oDataTableMapping.ColumnMappings.Add(oDataColumnMapping);
                }
                return oDataTableMapping;
            }
     
            /// <summary>
            /// crée le mapping d'une colonne
            /// </summary>
            /// <param name="SourceColumn"></param>
            /// <param name="DataSetColumn"></param>
            /// <returns></returns>
            public System.Data.Common.DataColumnMapping CreateDataColumnMapping(string SourceColumn, string DataSetColumn)
            {
                System.Data.Common.DataColumnMapping oDataColumnMapping = new System.Data.Common.DataColumnMapping(SourceColumn, DataSetColumn);
                return oDataColumnMapping;
            }
            /// <summary>
            /// éxécute une requête de mise à jour (insert,update,delete,...)
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <returns></returns>
            public int ExecuteNonQuery(System.Data.Common.DbCommand oDbCommand)
            {
                int nResult = 0;
                try
                {
                    oDbCommand.Connection.Open();
                    nResult = oDbCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbCommand.Connection.Close();
                }
                return nResult;
            }
            /// <summary>
            /// éxecute une requête select ne retournant qu'un résultat
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <returns></returns>
            public object ExecuteScalar(System.Data.Common.DbCommand oDbCommand)
            {
                object oResult = null;
                try
                {
                    oDbCommand.Connection.Open();
                    oResult = oDbCommand.ExecuteScalar();
     
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbCommand.Connection.Close();
                }
                return oResult;
            }
            /// <summary>
            /// éxecute un reader
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <returns></returns>
            public System.Data.Common.DbDataReader ExecuteReader(System.Data.Common.DbCommand oDbCommand)
            {
                System.Data.Common.DbDataReader oDbDataReader;
                try
                {
                    oDbCommand.Connection.Open();
                    oDbDataReader = oDbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbCommand.Connection.Close();
                }
                return oDbDataReader;
            }
            /// <summary>
            /// rempli un DataSet
            /// </summary>
            /// <param name="CommandText"></param>
            /// <param name="oDbConnection"></param>
            /// <param name="CommandType"></param>
            /// <returns></returns>
            public System.Data.DataSet FillDataSet(String CommandText, System.Data.Common.DbConnection oDbConnection, System.Data.CommandType CommandType)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataSet oDataSet = new System.Data.DataSet();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand.Connection = oDbConnection;
                    oDbDataAdapter.SelectCommand.CommandType = CommandType;
                    oDbDataAdapter.SelectCommand.CommandText = CommandText;
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataSet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataSet;
            }
            /// <summary>
            /// rempli un DataSet
            /// </summary>
            /// <param name="CommandText"></param>
            /// <param name="oDbConnection"></param>
            /// <param name="CommandType"></param>
            /// <param name="oDataTableMapping"></param>
            /// <returns></returns>
            public System.Data.DataSet FillDataSet(String CommandText, System.Data.Common.DbConnection oDbConnection, System.Data.CommandType CommandType, System.Data.Common.DataTableMapping oDataTableMapping)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataSet oDataSet = new System.Data.DataSet();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand.Connection = oDbConnection;
                    oDbDataAdapter.SelectCommand.CommandType = CommandType;
                    oDbDataAdapter.SelectCommand.CommandText = CommandText;
                    oDbDataAdapter.TableMappings.Add(oDataTableMapping);
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataSet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataSet;
            }
            /// <summary>
            /// rempli un DataSet
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <returns></returns>
            public System.Data.DataSet FillDataSet(System.Data.Common.DbCommand oDbCommand)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataSet oDataSet = new System.Data.DataSet();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = oDbCommand;
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataSet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataSet;
            }
            /// <summary>
            /// rempli un DataSet
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <returns></returns>
            public System.Data.DataSet FillDataSet(System.Data.Common.DbCommand oDbCommand, System.Data.Common.DataTableMapping oDataTableMapping)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataSet oDataSet = new System.Data.DataSet();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = oDbCommand;
                    oDbDataAdapter.TableMappings.Add(oDataTableMapping);
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataSet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataSet;
            }
            /// <summary>
            /// rempli une DataTable
            /// </summary>
            /// <param name="CommandText"></param>
            /// <param name="oDbConnection"></param>
            /// <param name="CommandType"></param>
            /// <returns></returns>
            public System.Data.DataTable FillDataTable(String CommandText, System.Data.Common.DbConnection oDbConnection, System.Data.CommandType CommandType)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataTable oDataTable = new System.Data.DataTable();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand.Connection = oDbConnection;
                    oDbDataAdapter.SelectCommand.CommandType = CommandType;
                    oDbDataAdapter.SelectCommand.CommandText = CommandText;
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataTable);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataTable;
            }
            /// <summary>
            /// rempli une DataTable
            /// </summary>
            /// <param name="CommandText"></param>
            /// <param name="oDbConnection"></param>
            /// <param name="CommandType"></param>
            /// <param name="oDataTableMapping"></param>
            /// <returns></returns>
            public System.Data.DataTable FillDataTable(String CommandText, System.Data.Common.DbConnection oDbConnection, System.Data.CommandType CommandType, System.Data.Common.DataTableMapping oDataTableMapping)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataTable oDataTable = new System.Data.DataTable();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand.Connection = oDbConnection;
                    oDbDataAdapter.SelectCommand.CommandType = CommandType;
                    oDbDataAdapter.SelectCommand.CommandText = CommandText;
                    oDbDataAdapter.TableMappings.Add(oDataTableMapping);
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataTable);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataTable;
            }
            /// <summary>
            /// rempli une DataTable
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <returns></returns>
            public System.Data.DataTable FillDataTable(System.Data.Common.DbCommand oDbCommand)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataTable oDataTable = new System.Data.DataTable();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = oDbCommand;
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataTable);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataTable;
            }
            /// <summary>
            /// rempli une DataTable
            /// </summary>
            /// <param name="oDbCommand"></param>
            /// <param name="oDataTableMapping"></param>
            /// <returns></returns>
            public System.Data.DataTable FillDataTable(System.Data.Common.DbCommand oDbCommand, System.Data.Common.DataTableMapping oDataTableMapping)
            {
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
                System.Data.DataTable oDataTable = new System.Data.DataTable();
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = oDbCommand;
                    oDbDataAdapter.TableMappings.Add(oDataTableMapping);
     
                    oDbDataAdapter.SelectCommand.Connection.Open();
                    oDbDataAdapter.Fill(oDataTable);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    oDbDataAdapter.SelectCommand.Connection.Close();
                }
                return oDataTable;
            }
            /// <summary>
            /// met à jour un DataSet
            /// </summary>
            /// <param name="SelectCommand"></param>
            /// <param name="InsertCommand"></param>
            /// <param name="UpdateCommand"></param>
            /// <param name="DeleteCommand"></param>
            /// <param name="ContinueUpdateOnError"></param>
            /// <returns></returns>
            public int UpdateDataSet(System.Data.Common.DbCommand SelectCommand, System.Data.Common.DbCommand InsertCommand, System.Data.Common.DbCommand UpdateCommand, System.Data.Common.DbCommand DeleteCommand, System.Data.DataSet oDataSet, bool ContinueUpdateOnError)
            {
                int nLignes = 0;
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = SelectCommand;
                    oDbDataAdapter.InsertCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.InsertCommand = InsertCommand;
                    oDbDataAdapter.UpdateCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.UpdateCommand = UpdateCommand;
                    oDbDataAdapter.DeleteCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.DeleteCommand = DeleteCommand;
     
                    oDbDataAdapter.ContinueUpdateOnError = ContinueUpdateOnError;
     
                    nLignes = oDbDataAdapter.Update(oDataSet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nLignes;
            }
            /// <summary>
            /// met à jour un DataSet
            /// </summary>
            /// <param name="SelectCommand"></param>
            /// <param name="InsertCommand"></param>
            /// <param name="UpdateCommand"></param>
            /// <param name="DeleteCommand"></param>
            /// <param name="oDataSet"></param>
            /// <param name="ContinueUpdateOnError"></param>
            /// <param name="oDataTableMapping"></param>
            /// <returns></returns>
            public int UpdateDataSet(System.Data.Common.DbCommand SelectCommand, System.Data.Common.DbCommand InsertCommand, System.Data.Common.DbCommand UpdateCommand, System.Data.Common.DbCommand DeleteCommand, System.Data.DataSet oDataSet, bool ContinueUpdateOnError, System.Data.Common.DataTableMapping oDataTableMapping)
            {
                int nLignes = 0;
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = SelectCommand;
                    oDbDataAdapter.InsertCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.InsertCommand = InsertCommand;
                    oDbDataAdapter.UpdateCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.UpdateCommand = UpdateCommand;
                    oDbDataAdapter.DeleteCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.DeleteCommand = DeleteCommand;
                    oDbDataAdapter.TableMappings.Add(oDataTableMapping);
     
                    oDbDataAdapter.ContinueUpdateOnError = ContinueUpdateOnError;
     
                    nLignes = oDbDataAdapter.Update(oDataSet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nLignes;
            }
            /// <summary>
            /// met à jour une DataTable
            /// </summary>
            /// <param name="SelectCommand"></param>
            /// <param name="InsertCommand"></param>
            /// <param name="UpdateCommand"></param>
            /// <param name="DeleteCommand"></param>
            /// <param name="ContinueUpdateOnError"></param>
            /// <returns></returns>
            public int UpdateDataTable(System.Data.Common.DbCommand SelectCommand, System.Data.Common.DbCommand InsertCommand, System.Data.Common.DbCommand UpdateCommand, System.Data.Common.DbCommand DeleteCommand, System.Data.DataTable oDataTable, bool ContinueUpdateOnError)
            {
                int nLignes = 0;
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = SelectCommand;
                    oDbDataAdapter.InsertCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.InsertCommand = InsertCommand;
                    oDbDataAdapter.UpdateCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.UpdateCommand = UpdateCommand;
                    oDbDataAdapter.DeleteCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.DeleteCommand = DeleteCommand;
     
                    oDbDataAdapter.ContinueUpdateOnError = ContinueUpdateOnError;
     
                    nLignes = oDbDataAdapter.Update(oDataTable);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nLignes;
            }
            /// <summary>
            /// met à jour une DataTable
            /// </summary>
            /// <param name="SelectCommand"></param>
            /// <param name="InsertCommand"></param>
            /// <param name="UpdateCommand"></param>
            /// <param name="DeleteCommand"></param>
            /// <param name="oDataTable"></param>
            /// <param name="ContinueUpdateOnError"></param>
            /// <param name="oDataTableMapping"></param>
            /// <returns></returns>
            public int UpdateDataTable(System.Data.Common.DbCommand SelectCommand, System.Data.Common.DbCommand InsertCommand, System.Data.Common.DbCommand UpdateCommand, System.Data.Common.DbCommand DeleteCommand, System.Data.DataTable oDataTable, bool ContinueUpdateOnError, System.Data.Common.DataTableMapping oDataTableMapping)
            {
                int nLignes = 0;
                System.Data.Common.DbDataAdapter oDbDataAdapter = null;
     
                try
                {
                    oDbDataAdapter = _DbProviderFactory.CreateDataAdapter();
                    oDbDataAdapter.SelectCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.SelectCommand = SelectCommand;
                    oDbDataAdapter.InsertCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.InsertCommand = InsertCommand;
                    oDbDataAdapter.UpdateCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.UpdateCommand = UpdateCommand;
                    oDbDataAdapter.DeleteCommand = DbProviderFactory.CreateCommand();
                    oDbDataAdapter.DeleteCommand = DeleteCommand;
                    oDbDataAdapter.TableMappings.Add(oDataTableMapping);
     
                    oDbDataAdapter.ContinueUpdateOnError = ContinueUpdateOnError;
     
                    nLignes = oDbDataAdapter.Update(oDataTable);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nLignes;
            }
            /// <summary>
            /// commence une transaction
            /// </summary>
            /// <param name="oDbConnection"></param>
            /// <returns></returns>
            public System.Data.Common.DbTransaction BeginTransaction(System.Data.Common.DbConnection oDbConnection)
            {
                try
                {
                    System.Data.Common.DbTransaction oDbTransaction = oDbConnection.BeginTransaction();
                    return oDbTransaction;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            /// valide la transaction
            /// </summary>
            /// <param name="oDbTransaction"></param>
            public void CommitTransaction(System.Data.Common.DbTransaction oDbTransaction)
            {
                try
                {
                    oDbTransaction.Commit();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            /// annule la transaction
            /// </summary>
            /// <param name="oDbTransaction"></param>
            public void RollbackTransaction(System.Data.Common.DbTransaction oDbTransaction)
            {
                try
                {
                    oDbTransaction.Rollback();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    J'ai une crée une windows form dans laquelle j'ai ajouté une datagridview et un bouton et j'ai codé le bouton comme suit:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
           public partial class MainForm : Form
        {
            //InventoryDALDisLayer dal = null;
            DbProviderUtilities oDbProviderUtilities;
            System.Data.Common.DbConnection oDbConnection;
     
            public MainForm()
            {
                InitializeComponent();
            }
     
            private void MainForm_Load(object sender, EventArgs e)
            {
                oDbProviderUtilities = new DbProviderUtilities();
                oDbProviderUtilities.GetConfigurationConnections();
                oDbConnection = oDbProviderUtilities.CreateConnection();
            }
     
     
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    this.FillDataSet();
     
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
     
            public void FillDataSet()
            {
                System.Data.DataSet oDataSet = oDbProviderUtilities.FillDataSet("Select * from maTable", oDbConnection, CommandType.Text);
     
                inventoryGrid.DataSource = oDataSet.Tables[0];
            }
     
        }
    Mon fichier App.config ressemble à celà :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <configuration>
      <appSettings>
        <add key="provider" value="Oracle.DataAccess.Client" />
      </appSettings>
     
      <!-- Here are the connection strings -->
      <connectionStrings>
        <add name ="OracleProvider"  connectionString =
      "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=servicename)));User Id=userid;Password=password;"/>
     
      </connectionStrings>
    </configuration>
    Et lorsque j'exécute le code et je clique sur mon bouton, j'ai le message suivant:

    La référence d'objet n'est pas définie à une instance d'un objet.

    Merci,

Discussions similaires

  1. [Data] problème de connections base de donné oracle 10g
    Par bourbah dans le forum Spring
    Réponses: 0
    Dernier message: 29/03/2011, 17h17
  2. Connection a une base de données Oracle
    Par dim971 dans le forum Accès aux données
    Réponses: 4
    Dernier message: 12/03/2007, 11h50
  3. Réponses: 6
    Dernier message: 08/11/2006, 14h05
  4. Réponses: 4
    Dernier message: 22/12/2005, 11h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo