【问题标题】:DataAdapter.Update() returns 1 without updating databaseDataAdapter.Update() 返回 1 而不更新数据库
【发布时间】:2012-04-04 16:13:24
【问题描述】:

抱歉,如果之前已经回答过此问题,但我无法在任何地方找到解决方案。这很可能是显而易见的,但我现在被自己的代码蒙蔽了。

我正在尝试使用某些文本框中的信息更新我的数据库。在调试时,我可以看到 DataTable 行正在使用更改进行更新,并且 dataadapter.update 返回 1。但是当我检查我的数据库后,它没有任何更改。奇怪的是,它在我的第一个更新调用中起作用,但在另一个调用中不起作用。

我在使用后关闭连接。

我尝试了几种不同的方法来实现这一点,但无论我尝试什么,我都无法让它发挥作用。我尽可能地创建了它(因为我是新手)与工作的人一样,但仍然没有。我也尝试为更新创建自己的更新命令。

这是我的命令:

    private SqlCeCommand createCharInstanceCommand()
    {
        SqlCeCommand updateCommand = new SqlCeCommand();
        updateCommand.CommandText = "UPDATE [CharacterClassInstance] SET " +
            "[CharID] = @p1, [ClassID] = @p2, [Lvl] = @p3, [SpellsKnown0] = @p4, [SpellsKnown1] = @p5, [SpellsKnown2] = @p6, " +
            "[SpellsKnown3] = @p7, [SpellsKnown4] = @p8, [SpellsKnown5] = @p9, [SpellsKnown6] = @p10, [SpellsKnown7] = @p11, " +
            "[SpellsKnown8] = @p12, [SpellsKnown9] = @p13, [SpellsPrDay0] = @p14, [SpellsPrDay1] = @p15, [SpellsPrDay2] = @p16, " +
            "[SpellsPrDay3] = @p17, [SpellsPrDay4] = @p18, [SpellsPrDay5] = @p19, [SpellsPrDay6] = @p20, [SpellsPrDay7] = @p21, " +
            "[SpellsPrDay8] = @p22, [SpellsPrDay9] = @p23, [SpellSaveDC0] = @p24, [SpellSaveDC1] = @p25, [SpellSaveDC2] = @p26, " +
            "[SpellSaveDC3] = @p27, [SpellSaveDC4] = @p28, [SpellSaveDC5] = @p29, [SpellSaveDC6] = @p30, [SpellSaveDC7] = @p31, " +
            "[SpellSaveDC8] = @p32, [SpellSaveDC9] = @p33, [BonusSpell1] = @p34, [BonusSpell2] = @p35, [BonusSpell3] = @p36, " +
            "[BonusSpell4] = @p37, [BonusSpell5] = @p38, [BonusSpell6] = @p39, [BonusSpell7] = @p40, [BonusSpell8] = @p41, [BonusSpell9] = @p42 " +
            "WHERE ([ClassInstanceID] = @p43)";

        for (int i = 1; i <=43;i++)
        {
            if (i == 43) updateCommand.Parameters.AddWithValue("@p43", chosenClassInstance);
            else updateCommand.Parameters.AddWithValue("@p" + i, dClassInstance.Rows[chosenClassInstance - 1].ItemArray.GetValue(i).ToString());
        }
        return updateCommand;
    }

这是应该更新表的代码(我认为是版本 nr 8)。我添加了 rowmodified 作为我最后的尝试之一,因为不确定该行是否由于某种原因注册为已修改。

    private void updateClassInstance()
    {
        DataRow row = dClassInstance.Rows[chosenClassInstance - 1];

        for (int i = 4; i <= 42; i++)
        {
            if (i <= 23) row[i] = Convert.ToInt32(spellInfoBoxes[i - 4].Text);
            if (i >= 34) row[i] = Convert.ToInt32(spellInfoBoxes[i - 14].Text);
        }
        try 
        { 
            dClassInstance.Rows[chosenClassInstance - 1].AcceptChanges(); 
            dClassInstance.Rows[chosenClassInstance - 1].SetModified(); 
        }
        catch { }

        int spellinfotest = this.classDataAdapter.Update(dClassInstance);
        this.classConnection.Close();
    }

数据适配器和命令的创建。

    private void fillSpellInfo()
    {
        classConnection = new SqlCeConnection(sConnectionString);

        string sqlClassInstance = "SELECT * " +
                                    "FROM CharacterClassInstance " +
                                    "WHERE (CharID = " + iCharacterID + 
                                    //") AND (ClassInstanceID = "+chosenClassInstance+
                                    ")";

        SqlCeCommand selectClassInstance = classConnection.CreateCommand();
        selectClassInstance.CommandText = sqlClassInstance;

        this.classConnection.Open();

        classDataAdapter = new SqlCeDataAdapter(selectClassInstance);
        //SqlCeCommandBuilder cb = new SqlCeCommandBuilder(classDataAdapter);



        //classDataAdapter.UpdateCommand = cb.GetUpdateCommand();


        dClassInstance = new DataTable();

        classDataAdapter.Fill(dClassInstance);

        classDataAdapter.UpdateCommand = createCharInstanceCommand();

        dClassInstance.DefaultView.RowFilter = "ClassInstanceID = " + chosenClassInstance;

        setSpellInfoValues();
        this.classConnection.Close();
    }

由于某种原因,它适用于另一个数据适配器上的标准生成命令。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# sql-server-ce dataadapter sql-update


    【解决方案1】:

    我终于弄清楚了为什么我的代码不起作用。 问题在于 UpdateCommand 的构建,或者更确切地说是参数的分配。在我有限的知识范围内,我盲目地遵循了一个示例,直到我自己为另一个字段集合编写更新时才看到错误。

    发生的情况是参数获得了旧值,因为它们是在我构建 DataAdapter 时添加的,而不是在更新完成后添加的。我怀疑分配变量的方法比 addWithValue() 更好,但至少它现在可以工作;) 生活和学习有人说:)

    为了解决我的问题,我移动了这段代码

    classDataAdapter.UpdateCommand = createCharInstanceCommand();
    

    进入我实际运行更新的部分:

            private void updateClassInstance()
        {
            DataRow row = dClassInstance.Rows[chosenClassInstance - 1];
    
            for (int i = 4; i <= 42; i++)
            {
                if (i <= 23) row[i] = Convert.ToInt32(spellInfoBoxes[i - 4].Text);
                if (i >= 34) row[i] = Convert.ToInt32(spellInfoBoxes[i - 14].Text);
            }
            try
            {
                dClassInstance.Rows[chosenClassInstance - 1].AcceptChanges();
                dClassInstance.Rows[chosenClassInstance - 1].SetModified();
            }
            catch { }
    
            classDataAdapter.UpdateCommand = createCharInstanceCommand();
            classDataAdapter.UpdateCommand.Connection = classConnection;
    
            int spellinfotest = this.classDataAdapter.Update(dClassInstance);
            this.classConnection.Close();
        }
    

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2013-10-25
      相关资源
      最近更新 更多