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