简体   繁体   English

C#Ole.DB错误

[英]C# Ole.DB Error

I'm creating a program in C# to help keep track of all my information instead of writing it down for my DND type game. 我正在用C#创建一个程序,以帮助跟踪我的所有信息,而不是为我的DND类型游戏写下来。 I am able to Insert and Delete new players without any problems but when it comes to update the players this is the error i get 我能够毫无问题地插入和删除新玩家,但是当更新玩家时,这就是我得到的错误

"Syntax error (missing operator) in query expression '@newPlayerName playerLevel = @newPlayerLevel heroID = @newHeroId playerInventoryID = @newPlayerInventoryID campaignID = @newCampaignID'." “查询表达式中的语法错误(缺少运算符)'@ newPlayerName playerLevel = @newPlayerLevel heroID = @newHeroId playerInventoryID = @newPlayerInventoryID campaignID = @newCampaignID'。”

here is my code for the update method 这是我的更新方法的代码

    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();
        }
    }

And here is the code that is calling that function 这是调用该函数的代码

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);                                       
            }
        }

Add commas 添加逗号

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 ";

You are missing all the commas between the fields updated 您缺少更新的字段之间的所有逗号

  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 "; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM