gpt4 book ai didi

c# - C#SqlDataReader引发语法错误,但是命令在SQL Server中运行正常?

转载 作者:行者123 更新时间:2023-12-03 08:12:54 26 4
gpt4 key购买 nike

我从下面的代码所示的SQL命令中收到语法错误。错误发生在ExecuteReader行上,说

Incorrect syntax near the keyword 'JOIN'.'



我不知道为什么会引发语法错误,该命令在SQL Server中工作得很好。这是代码:
private void button1_Click(object sender, EventArgs e)
{
SqlCommand sqlcomViewSmashRoster;
SqlDataReader dataReader;
String strSql, strOutput ="";

strSql = "SELECT Roster.CharacterName, Roster.TotalTournamentsParticiaped, Roster.TotalWins, Roster.TotalLosses, Roster.Championships, Roster.InjuriesCaused, Roster.Injured" +
"FROM Roster" +
"INNER JOIN VideoGames" +
"ON VideoGames.VideoGame_ID = Roster.VideoGame_ID" +
"WHERE roster.VideoGame_ID = 2";

cnn.Open();

sqlcomViewSmashRoster = new SqlCommand(strSql, cnn);

dataReader = sqlcomViewSmashRoster.ExecuteReader();

while (dataReader.Read())
{
strOutput = strOutput + dataReader.GetValue(0) + " - " + dataReader.GetValue(1) + "\n";
}

cnn.Close();

MessageBox.Show(strOutput);
}

谢谢!

最佳答案

空格。请记住:

"abc" +
"def"

"abcdef"
逐字字符串文字是您的 friend :

strSql = @"
SELECT Roster.CharacterName, Roster.TotalTournamentsParticiaped, Roster.TotalWins,
Roster.TotalLosses, Roster.Championships, Roster.InjuriesCaused, Roster.Injured
FROM Roster
INNER JOIN VideoGames
ON VideoGames.VideoGame_ID = Roster.VideoGame_ID
WHERE roster.VideoGame_ID = 2
";


首先,这不仅易于编码,而且您可以轻松地在SSMS和devenv之间复制/粘贴,而无需添加引号等。

关于c# - C#SqlDataReader引发语法错误,但是命令在SQL Server中运行正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com