1

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

“查询表达式'@newPlayerName playerLevel = @newPlayerLevel heroID = @newHeroId playerInventoryID = @newPlayerInventoryIDcampaignID = @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);                                       
            }
        }
4

2 回答 2

2

添加逗号

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 ";
于 2012-08-16T20:01:17.663 回答
1

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

  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 "; 
于 2012-08-16T20:01:55.480 回答