C# Ole.DB Error

  • 本文关键字:Error DB Ole c#
  • 更新时间 :
  • 英文 :


我正在用C#创建一个程序来帮助跟踪我的所有信息,而不是为我的DND类型游戏写下来。我可以毫无问题地插入和删除新播放器,但是在更新播放器时,这是我遇到的错误

"查询表达式'@newPlayerName玩家级别 = @newPlayerLevel heroID = @newHeroId 玩家库存 ID = @newPlayerInventoryID 广告系列 ID = @newCampaignID'中的语法错误(缺少运算符)。

这是我的更新方法的代码

    public static Boolean UpdatePlayer(Player oldPlayer,
        Player newPlayer)
    {
        OleDbConnection connection = DBConnection.GetConnection();
        OleDbCommand command;
        string statement =
            "UPDATE Player SET " +
            "playerName = @newPlayerName " +
            "playerLevel = @newPlayerLevel " +
            "heroID = @newHeroId " +
            "playerInventoryID = @newPlayerInventoryID " +
            "campaignID = @newCampaignID " +
            "WHERE ID = @oldID " +
            "AND playerName = @oldPlayerName " +
            "AND playerLevel = @oldPlayerLevel " +
            "AND heroID = @oldHeroID " +
            "AND playerInventoryID = @oldPlayerInventoryID " +
            "AND campaignID = @oldCampaignID ";
        command = new OleDbCommand(statement, connection);
        command.Parameters.AddWithValue("@newPlayerName", newPlayer.PlayerName);
        command.Parameters.AddWithValue("@newPlayerLevel", newPlayer.Level);
        command.Parameters.AddWithValue("@newHeroID", newPlayer.HeroID);
        command.Parameters.AddWithValue("@newPlayerInventoryID", newPlayer.PlayerInventoryID);
        command.Parameters.AddWithValue("@newCampaignID", newPlayer.CampaignID);
        command.Parameters.AddWithValue("@oldID", oldPlayer.ID);
        command.Parameters.AddWithValue("@oldPlayerName", oldPlayer.PlayerName);
        command.Parameters.AddWithValue("@oldPlayerLevel", oldPlayer.Level);
        command.Parameters.AddWithValue("@oldHeroID", oldPlayer.HeroID);
        command.Parameters.AddWithValue("@oldPlayerInventoryID", oldPlayer.PlayerInventoryID);
        command.Parameters.AddWithValue("@oldCampaignID", oldPlayer.CampaignID);
        try
        {
            connection.Open();
            int count = command.ExecuteNonQuery();
            if (count > 0)
                return true;
            else
                return false;
        }
        catch (OleDbException e)
        {
            throw e;
        }
        finally
        {
            connection.Close();
        }
    }

这是调用该函数的代码

private void SavePlayers()
    {
        if (lstPlayers.SelectedIndex > -1)
        {
            int parsedInt;
            Player edititedPlayer = new Player();
            edititedPlayer.PlayerName = txtPlayerName.Text;
            edititedPlayer.HeroID =
                heroes[cboHero.SelectedIndex].ID;
            edititedPlayer.CampaignID =
                campaigns[lstCampaigns.SelectedIndex].ID;
            edititedPlayer.ID = players[lstPlayers.SelectedIndex].ID;
            edititedPlayer.PlayerInventoryID = 
                players[lstPlayers.SelectedIndex].PlayerInventoryID;
            if (Int32.TryParse(txtPlayerLevel.Text, out parsedInt))
            {
                edititedPlayer.Level = parsedInt;
                PlayerDB.UpdatePlayer((Player)lstPlayers.SelectedItem,
                    edititedPlayer);                                       
            }
        }

添加逗号

string statement =
    "UPDATE Player SET " +
    "playerName = @newPlayerName, " +
    "playerLevel = @newPlayerLevel, " +
    "heroID = @newHeroId, " +
    "playerInventoryID = @newPlayerInventoryID, " +
    "campaignID = @newCampaignID " +
    "WHERE ID = @oldID " +
    "AND playerName = @oldPlayerName " +
    "AND playerLevel = @oldPlayerLevel " +
    "AND heroID = @oldHeroID " +
    "AND playerInventoryID = @oldPlayerInventoryID " +
    "AND campaignID = @oldCampaignID ";

您缺少更新的字段之间的所有逗号

  string statement = 
        "UPDATE Player SET " + 
        "playerName = @newPlayerName, " + 
        "playerLevel = @newPlayerLevel, " + 
        "heroID = @newHeroId, " + 
        "playerInventoryID = @newPlayerInventoryID, " + 
        "campaignID = @newCampaignID " + 
        "WHERE ID = @oldID " + 
        "AND playerName = @oldPlayerName " + 
        "AND playerLevel = @oldPlayerLevel " + 
        "AND heroID = @oldHeroID " + 
        "AND playerInventoryID = @oldPlayerInventoryID " + 
        "AND campaignID = @oldCampaignID "; 

最新更新