我有一张名为Town的桌子,它掌握着玩家和玩家建筑物之间的关系。该表包含以下列: userid(int) buildingid(int) number(int)
我已成功为玩家插入新建筑物,现在我无法更新播放器的特定行。
我有这段代码:
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!!!!";
}
如果玩家已经拥有具有给定id的建筑物,则existsPlayerBuilding方法返回true,否则返回false。当我运行并测试我是否可以为用户构建更新号码时没有任何反应。
答案 0 :(得分:10)
SQL中的更新语法为
UPDATE Town
SET number = @number
WHERE userid = @userid AND buildingid = @buildingid
答案 1 :(得分:6)
您的更新查询错误:
UPDATE Town
SET number = @number
WHERE userid = @userid AND buildingid = @buildingid