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

Windows Forms Discussion :

[C#] Comment mettre à jour une base de données Oracle depuis mon Dataset?


Sujet :

Windows Forms

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 46
    Points : 40
    Points
    40
    Par défaut [C#] Comment mettre à jour une base de données Oracle depuis mon Dataset?
    Bonjour !!

    Je viens chercher de l'aide car je commence à craquer ...
    Voila je dois mettre à jour (insertion, modification, suppression) une base de données (Oracle) à partir d'un fichier Excel. Concrétement voici le code :

    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
    DataSet MyDs = new DataSet();
            OracleDataAdapter MonAdapter;
            OracleCommandBuilder MonBuilder;
     
            try
            {
                string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NomFic + @";Extended Properties=""Excel 8.0;HDR=YES;""";
     
     
                DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
     
                DbDataAdapter adapter = factory.CreateDataAdapter();
     
                DbCommand selectCommand = factory.CreateCommand();
                selectCommand.CommandText = "SELECT Matricule, Nom, Prénom, [C unité appartenance] FROM [EFFECTIF$]";
     
                DbConnection connection = factory.CreateConnection();
                connection.ConnectionString = excelConnectionString;
     
                selectCommand.Connection = connection;
     
                adapter.SelectCommand = selectCommand;
     
                adapter.Fill(MyDs);
     
                MonAdapter = new OracleDataAdapter("Select Matricule, Nom, Prénom, MPE from system.salarie", "Data Source=MaBase;User ID=L'ID;Password=PWD;"); 
    // MaBase, L'ID et PWD sont remplacer dans le vrai code
     
                MonBuilder = new OracleCommandBuilder(MonAdapter);
     
                MonAdapter.Update(MyDs);
     
               //blabla suite du programme
    J'ai essayé aussi via les commandes insert, update, delete du dataadapter, mais ca marche pas. En fait tout se passe bien (pas d'exception levée, et la suite du code est exécutée normalement), mais les données de la base ne sont pas mises à jour Le dataset est remmpli (je l'affiche apres dans un datagrid sur ma page)

    Quelqu'un a une idée svp ??

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    46
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 46
    Points : 40
    Points
    40
    Par défaut
    Ben j'ai un peu avancé en modifiant mon code, mais ca ne marche toujours pas. J'ai essayé avec un commandbuilder et en définissant les selectcommand, insertcommand, updatecommand et deletecommand, mais ca marche pas non plus. La fonction update revoie toujours 0 (nb de ligne mises à jour).

    voici la denière version de mon code :
    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
             OracleCommand MaCom;
            DataSet MyDs = new DataSet();
            OracleDataAdapter MonAdapter;
            OracleConnection MaCon;
     
            try
            {
                // chaine de connexion pour excel
                string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NomFic + 
                                                @";Extended Properties=""Excel 8.0;HDR=YES;""";
     
                // création du provider pour excel
                DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
     
                // création de l'adapter
                DbDataAdapter adapter = factory.CreateDataAdapter();
     
                // création de la commande de selection
                DbCommand selectCommand = factory.CreateCommand();
                selectCommand.CommandText = "SELECT Matricule, Nom, Prénom, [C unité appartenance] " +
                                            "FROM [EFFECTIF$]";
     
                DbConnection connection = factory.CreateConnection();
                connection.ConnectionString = excelConnectionString;
     
                selectCommand.Connection = connection;
     
                adapter.SelectCommand = selectCommand;
     
                // remplissage de mon dataset
                adapter.Fill(MyDs);
     
                //création de mon adapteur Oracle
                MonAdapter = new OracleDataAdapter();
     
                // initialisation de la connexion
                MaCon = new OracleConnection("Data Source=Source;User ID=ID;Password=PWD;");
     
                // création de la commande de selection
                MaCom = new OracleCommand("Select Matricule, Nom, Prenom, MPE from system.salarie", MaCon);
                MonAdapter.SelectCommand = MaCom;
     
                // création de la commande d'insert
                MaCom = new OracleCommand("insert into system.salarie (Matricule, Nom, " +
                                                            "Prenom, MPE) values (?, ?, ?, ?)", MaCon);
                // Ajout des paramètres
                MaCom.Parameters.Add("Matricule", OracleDbType.Int32);
                MaCom.Parameters.Add("Nom", OracleDbType.Varchar2);
                MaCom.Parameters.Add("Prénom", OracleDbType.Varchar2);
                MaCom.Parameters.Add("[C unité appartenance]", OracleDbType.Varchar2);
     
                // Affectation de la commande MaCom à la propriété insertcommand de MonAdapter
                MonAdapter.InsertCommand = MaCom;
     
                // création de la commande d'update
                MaCom = new OracleCommand("Update system.salarie set MPE = ? where Matricule = ?", MaCon);
     
                MaCom.Parameters.Add("[C unité appartenance]", OracleDbType.Varchar2);
                MaCom.Parameters.Add("Matricule", OracleDbType.Int32);
     
                MonAdapter.UpdateCommand = MaCom;
     
                // Création de la commande de suppression
                MaCom = new OracleCommand("delete from system.salarie where Matricule = ?", MaCon);
                MaCom.Parameters.Add("Matricule", OracleDbType.Int32);
     
                MonAdapter.DeleteCommand = MaCom;
     
                //Mise à jour de la base et affichage du nombre de ligne modifiées, la table [Effectif$] de mon dataset s'appelle Table, (mais je n'ai pas compris pourquoi)
                MessageBox.Show(MonAdapter.Update(MyDs, "Table").ToString());
    ou avec un CommandBuilder :

    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
        OracleCommandBuilder MonBuilder;
            OracleCommand MaCom;
            DataSet MyDs = new DataSet();
            OracleDataAdapter MonAdapter;
            OracleConnection MaCon;
     
            try
            {
                // chaine de connexion pour excel
                string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NomFic + 
                                                @";Extended Properties=""Excel 8.0;HDR=YES;""";
     
                // création du provider pour excel
                DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
     
                // création de l'adapter
                DbDataAdapter adapter = factory.CreateDataAdapter();
     
                // création de la commande de selection
                DbCommand selectCommand = factory.CreateCommand();
                selectCommand.CommandText = "SELECT Matricule, Nom, Prénom, [C unité appartenance] " +
                                            "FROM [EFFECTIF$]";
     
                DbConnection connection = factory.CreateConnection();
                connection.ConnectionString = excelConnectionString;
     
                selectCommand.Connection = connection;
     
                adapter.SelectCommand = selectCommand;
     
                // remplissage de mon dataset
                adapter.Fill(MyDs);
     
                //création de mon adapteur Oracle
                MonAdapter = new OracleDataAdapter();
     
                // initialisation de la connexion
                MaCon = new OracleConnection("Data Source=Source;User ID=ID;Password=PWD;");
     
                // création de la commande de selection
                MaCom = new OracleCommand("Select Matricule, Nom, Prenom, MPE from system.salarie", MaCon);
                MonAdapter.SelectCommand = MaCom;
     
                MonBuilder = new OracleCommandBuilder(MonAdapter);
     
                //Mise à jour de la base et affichage du nombre de ligne modifiées
                MessageBox.Show(MonAdapter.Update(MyDs, "Table").ToString());
    PS : les chaines de connexion ont été modifié volontairement.
    Sinon mon dataset et rempli, je peux afficher les données venues du fichier Excel, et mon adapter marche, car je peux remplir un dataset et l'afficher ensuite.

    Merci

Discussions similaires

  1. Réponses: 1
    Dernier message: 16/12/2010, 14h34
  2. Réponses: 3
    Dernier message: 13/07/2009, 19h07
  3. Réponses: 1
    Dernier message: 05/04/2008, 11h28
  4. Comment mettre à jour une base de données access
    Par nicolas2603 dans le forum VB.NET
    Réponses: 11
    Dernier message: 16/10/2007, 14h40
  5. [SQL SERVER] Mettre à jour une base de donnée
    Par grellierj dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 24/05/2006, 11h33

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