' System.Data.SqlClient.SqlException'发生在System.Data.dll中但未在用户代码中处理。附加信息:' b'附近的语法不正确

时间:2016-04-03 05:13:48

标签: c# sql-server

我一直收到此错误,我已多次检查我的代码以找到解决方案,但无济于事。

private void PopulateotherNames()
{
    string query = "SELECT a nickName FROM otherNames a" +
                   "INNER JOIN nameNicki b ON a.Id = b.otherNamesId" +
                   "WHERE b.realNameId=@realNameId";

    using (connection = new SqlConnection(connectionString))
    using (SqlCommand command = new SqlCommand(query, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
    {
        command.Parameters.AddWithValue("@realNameId", listRealName.SelectedValue);

        DataTable NickNameTable = new DataTable();
        adapter.Fill(NickNameTable);

        listNickName.DisplayMember = "nickName";
        listNickName.ValueMember = "Id";
        listNickName.DataSource = NickNameTable;
    }
}

1 个答案:

答案 0 :(得分:2)

试试这个...错过你的别名,在某些地方没有空格。查看存储过程,您将不会遇到任何此类问题。

string query = "SELECT a.nickName FROM otherNames a " +
        "INNER JOIN nameNicki b ON " +
        "a.Id = b.otherNamesId" +
        " WHERE b.realNameId=@realNameId";