我正在尝试保留一张记录表,以查看玩家输给其他玩家的次数,但在记录栏中,这些数字会被减去。
我的输出:
Name Won Lost Record
-----------------------------------
Player 1 4 3 1
Player 2 3 4 -1
期望的输出:
Name Won Lost Record
-----------------------------------
Player 1 4 3 4-3
Player 2 3 4 3-4
以下是我给出的输出代码:
string columnBeat1 = "[BEAT " + tbl1_p2Txt.Text.ToUpper() + "]";
string columnBeat2 = "[BEAT " + tbl1_p1Txt.Text.ToUpper() + "]";
string columnLost1 = "[LOST TO " + tbl1_p2Txt.Text.ToUpper() + "]";
string columnLost2 = "[LOST TO " + tbl1_p1Txt.Text.ToUpper() + "]";
string columnRecordBeat = "[RECORD VS " + tbl1_p2Txt.Text.ToUpper() + "]";
string columnRecordLost = "[RECORD VS " + tbl1_p1Txt.Text.ToUpper() + "]";
SqlCommand cmdBeat = new SqlCommand("UPDATE dbo.[RECORD] SET " + columnBeat1 + " = " + columnBeat1 + " + 1 " + " WHERE [NAME] = @Name", con);
SqlCommand cmdLost = new SqlCommand("UPDATE dbo.[RECORD] SET " + columnLost2 + " = " + columnLost2 + " + 1 " + " WHERE [NAME] = @Name", con);
SqlCommand cmdRecordBeat = new SqlCommand("UPDATE dbo.[RECORD] SET " + columnRecordBeat + " = " + columnBeat1 + '-' + columnLost1 + " WHERE [NAME] = @Name", con);
SqlCommand cmdRecordLost = new SqlCommand("UPDATE dbo.[RECORD] SET " + columnRecordLost + " = " + columnBeat2 + '-' + columnLost2 + " WHERE [NAME] = @Name", con);
cmdBeat.CommandType = CommandType.Text;
cmdLost.CommandType = CommandType.Text;
cmdRecordBeat.CommandType = CommandType.Text;
cmdRecordLost.CommandType = CommandType.Text;
con.Open();
cmdBeat.Parameters.AddWithValue("@Name", tbl1_p1Txt.Text);
cmdBeat.ExecuteNonQuery();
cmdLost.Parameters.AddWithValue("@Name", tbl1_p2Txt.Text);
cmdLost.ExecuteNonQuery();
cmdRecordBeat.Parameters.AddWithValue("@Name", tbl1_p1Txt.Text);
cmdRecordBeat.ExecuteNonQuery();
cmdRecordLost.Parameters.AddWithValue("@Name", tbl1_p2Txt.Text);
cmdRecordLost.ExecuteNonQuery();
con.Close();
答案 0 :(得分:0)
因为输入不是字符串。使用单引号或使用sqlparameter
设置它答案 1 :(得分:0)
正如@FrankX在他的回答中所述,问题在于你处理的是数字,而不是字符串。在两个数字之间加一个减号通常意味着你要减去 - 这里的SQL不是例外。
让我们首先考虑如何在C#中执行此操作。
int winCount = GetWinCount(); // get the value 4 from somewhere, e.g. the database
int lossCount = GetLostCount(); // get the value 3 from somewhere, e.g. the database
string resultColumnName = "Result";
string winsColumnName = "Wins"; // we don't actually need this
string lossesColumnName = "Losses"; // we don't actually need this
// what we want is a string like this:
// Result='4-3'
string query = "UPDATE ... SET " + resultColumnName + " = '" + winCount + "-" + lossCount + "' WHERE ..."
现在,C#实际上隐藏了我们感兴趣的部分:winCount和lossCount是int
s,但我们需要string
s。这发生在幕后。构建查询的行中的+
以静默方式将这两个值分别转换为字符串“4”和“3”。
如果我们不想在C#中这样做,我们需要告诉数据库创建一个'X-Y'形式的字符串值,其中X和Y是转换为字符串的整数。我暂时没有在SQL中做过这种事情,所以我不能告诉你确切的语法,而是寻找CONVERT
和CONCAT
函数。
在构建查询字符串的C#中,您需要将winsColumnName
,lossesColumnName
与这些函数结合使用。从C#的角度来看,函数名称当然只是字符串。它看起来与此类似:
string query = "... " + resultColumnName + "=CONCAT(CONVERT(VARCHAR, " + winsColumnName + "), '-', CONVERT(VARCHAR, " + lossesColumnName + ")) ..."
答案 2 :(得分:0)
这是SQL Server
。尝试这样的事情:
SqlCommand cmdRecordBeat = new SqlCommand("UPDATE dbo.[RECORD] SET " + columnRecordBeat + " = " + CAST(columnBeat1 AS VARCHAR) + '-' + CAST(columnLost1 AS VARCHAR) + " WHERE [NAME] = @Name", con);
SqlCommand cmdRecordLost = new SqlCommand("UPDATE dbo.[RECORD] SET " + columnRecordLost + " = " + CAST(columnBeat2 AS VARCHAR) + '-' + CAST(columnLost2 AS VARCHAR) + " WHERE [NAME] = @Name", con);
希望它对你有所帮助。