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

ADO.NET Discussion :

Mettre à jour Excel avec les données d'un DataTable via OleDB [Débutant]


Sujet :

ADO.NET

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 35
    Points : 22
    Points
    22
    Par défaut Mettre à jour Excel avec les données d'un DataTable via OleDB
    Bonjour à tous,

    Je cherche à manipuler des données excel avec OleDB. J'arrive à importer les données et à rajouter des lignes, mais je n'arrive pas à les mettre à jour. J'ai écrit le code ci-dessous qui ne fonctionne pas et je n'arrive pas à comprendre pourquoi. Avez-vous une idée ou une piste de réflexion pour m'éclairer ?

    Merci beaucoup.

    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
     
     
    public void UpdateStructure(Structure UpdateStructure)
            {
                ExcelConnection c = new ExcelConnection();
                OleDbConnection con = c.EstablishConnection();
                OleDbCommand command = con.CreateCommand();
                command.CommandText = "Select * From [" + sheetStructures + "$]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                DataSet1.CSx_StructuresDataTable tblTarget = new DataSet1.CSx_StructuresDataTable();
                adapter.Fill(tblTarget);
     
                DataRow updateRow = tblTarget.Select("Structure_ID="+UpdateStructure.structureID).FirstOrDefault();
     
                if (updateRow != null)
                {
                    updateRow.BeginEdit();
     
                    updateRow["Structure_Name"] = UpdateStructure.structureName;
                    updateRow["Implementation_Date"] = UpdateStructure.implementationDate;
                    if (UpdateStructure.structureName == "Structure 0")
                    {
                        updateRow["Start_Structure"] = true;
                        updateRow["Start_Amount"] = UpdateStructure.startAmount;
                    }
                    else
                    {
                        updateRow["Start_Structure"] = false;
                        updateRow["Start_Amount"] = Convert.ToDouble(0);
                    }
     
                    updateRow.EndEdit();
                    tblTarget.AcceptChanges();
                }
     
                string strUpdateCommand = "UPDATE [" + sheetStructures + "$] SET "+
                    "Structure_Name = ?, " +
                    "StructureGroup_ID = ?, " +
                    "Implementation_Date = ?, " +
                    "Start_Structure = ?, " +
                    "Start_Amount = ? " +
                    "WHERE Structure_ID = ?";
     
                OleDbCommand updateCommand = new OleDbCommand(strUpdateCommand, con);
                updateCommand.Parameters.Add("Structure_Name",OleDbType.Char,150, "Structure_Name");
                updateCommand.Parameters.Add("StructureGroup_ID",OleDbType.Integer,150, "StructureGroup_ID");
                updateCommand.Parameters.Add("Implementation_Date",OleDbType.Date,150, "Implementation_Date");
                updateCommand.Parameters.Add("Start_Structure", OleDbType.Char, 150, "Start_Structure");
                updateCommand.Parameters.Add("Start_Amount",OleDbType.Double, 150, "Start_Amount");
                updateCommand.Parameters.Add("Structure_ID", OleDbType.Integer, 150, "Structure_ID");
     
                adapter.UpdateCommand = updateCommand;
     
                con.Open();
     
                adapter.Update(tblTarget);
     
                con.Close();
     
                return;
     
            }
    Note : ExcelConnection est une classe que j'ai créée pour stocker les données de connexion.

  2. #2
    Inactif  

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2012
    Messages
    4 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2012
    Messages : 4 904
    Points : 10 168
    Points
    10 168
    Billets dans le blog
    36
    À ma connaissance, le seul personnage qui a été diagnostiqué comme étant allergique au mot effort. c'est Gaston Lagaffe.

    Ô Saint Excel, Grand Dieu de l'Inutile.

    Excel n'a jamais été, n'est pas et ne sera jamais un SGBD, c'est pour cela que Excel s'appelle Excel et ne s'appelle pas Access junior.

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 35
    Points : 22
    Points
    22
    Par défaut
    Déjà consulté

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 35
    Points : 22
    Points
    22
    Par défaut
    J'ai compris d'où venait le problème.

    Il faut supprimer la ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
       tblTarget.AcceptChanges();
    J'ai enfin compris en lisant cet article sur CommandBuilders et cet article sur DataRowState et les point suivant :

    UpdateCommand
    Updates rows at the data source for all rows in the table with a RowState of Modified. Updates the values of all columns except for columns that are not updateable, such as identities or expressions. Updates all rows where the column values at the data source match the primary key column values of the row, and where the remaining columns at the data source match the original values of the row. For more information, see "Optimistic Concurrency Model for Updates and Deletes," later in this topic.
    et

    Modified 16
    The row has been modified and AcceptChanges() has not been called.

    Quand on cherche, on trouve.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Mettre à jour une base de données avec un fichier excel
    Par antoine.courtois dans le forum Langage SQL
    Réponses: 1
    Dernier message: 29/11/2011, 14h05
  2. Réponses: 13
    Dernier message: 11/10/2008, 12h24
  3. [MySQL] Comment mettre à jour en conservant les données actuelles d'un champs
    Par MisterMacPhisto dans le forum PHP & Base de données
    Réponses: 14
    Dernier message: 17/04/2007, 15h49
  4. Réponses: 3
    Dernier message: 18/07/2006, 17h37
  5. Réponses: 12
    Dernier message: 22/06/2006, 12h09

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