Chr*_*llo 0 c# sql-server sqldatareader syntax-error executereader
I'm getting a syntax error from my SQL command shown in the code below. The error is happening on the ExecuteReader line, saying
Incorrect syntax near the keyword 'JOIN'.'
I have no idea why it's throwing a syntax error, the command works perfectly fine in SQL Server. Here's the code:
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);
}
Run Code Online (Sandbox Code Playgroud)
Thanks!
Whitespace. Remember that:
"abc" +
"def"
Run Code Online (Sandbox Code Playgroud)
is "abcdef"
Verbatim string literals are your 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
";
Run Code Online (Sandbox Code Playgroud)
Not only is this easier to code in the first place, but you can easily copy/paste between SSMS and devenv, without having to add quotes etc.
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |