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.