我有一個表格叫Town,它包含玩家和玩家建築物之間的關係。該表具有這些列: 用戶ID(INT) buildingid(INT) 號(INT)在SQL c中不能更新表格#
我已經成功地插入了玩家一個全新的建築,現在我有麻煩更新特定行的球員。
我有這樣的代碼:如果一個球員已經與給定的ID,否則爲假建築
if (!existPlayerBuilding(userid, buildingid))
{
SqlConnection sqlcon = new SqlConnection(connectionString);
string query = "INSERT INTO Town VALUES(@userid, @buildingid, @number)";
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
sqlcom.Parameters.Add("@userid", userid);
sqlcom.Parameters.Add("@buildingid", buildingid);
sqlcom.Parameters.Add("@number", number);
try
{
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
catch (Exception i)
{
lblTestlabel.Text = "SQL FEILET";
}
lblTestlabel.Text = "Bygningen ble kjøpt!!!!";
}
else
{
SqlConnection sqlcon = new SqlConnection(connectionString);
string query = "UPDATE Town VALUES(@userid, @buildingid, @number) WHERE userid = @userid AND buildingid = @buildingid";
SqlCommand sqlcom = new SqlCommand(query, sqlcon);
sqlcom.Parameters.Add("@userid", userid);
sqlcom.Parameters.Add("@buildingid", buildingid);
sqlcom.Parameters.Add("@number", number);
try
{
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
}
catch (Exception i)
{
lblTestlabel.Text = "SQL FEILET";
}
lblTestlabel.Text = "Bygningen ble kjøpt!!!!";
}
的existPlayerBuilding方法返回true。當我運行並測試如果我可以更新一個用戶建設的號碼什麼也沒有發生。
這往往有助於Management Studio中運行有問題的SQL命令,以蘇斯出語法錯誤。 – RQDQ 2012-03-29 14:41:07