2012-08-16 43 views
1

我正在C#中創建一個程序,以幫助跟蹤我的所有信息,而不是爲我的DND類型遊戲寫下它。我能插入和刪除新玩家沒有任何問題,但是當涉及到更新玩家這是我得到的錯誤C#Ole.DB錯誤

「查詢表達式中的語法錯誤(缺少運算符)'@ newPlayerName playerLevel = @newPlayerLevel heroID = @ newHeroId playerInventoryID = @newPlayerInventoryID campaignID = @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);          
      } 
     } 
+0

我想補充我已經檢查了數據庫中的所有名稱,並且它們與我在這裏的內容匹配(顯然區分大小寫)。我知道在VB中我們將使用一個參數來指定每個變量的數據類型。我沒有看到這些例子在C#書,所以我假設他們不需要。 – 2012-08-16 19:56:00

+0

我在您的SQL中看不到任何逗號 – msmucker0527 2012-08-16 19:59:57

回答

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

哇,史蒂夫我覺得自己現在是一個白癡,非常感謝你的快速反應。看了幾個小時的代碼,甚至沒有注意到順便說一句,我試圖給你+1但是我的代表不夠高對不起......但是非常感謝你的迴應。 – 2012-08-16 20:11:13

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

抱歉,您之前沒有注意到您的帖子。謝謝您的回答。我再次試圖給你+1,但我的代表不夠高:( – 2012-08-16 20:19:14