异常:';System.Data.SqlClient.SqlException';在System.Data.

本文关键字:System Data SqlException 异常 SqlClient | 更新日期: 2024-09-25 14:47:45

我是C#的初学者,当我执行代码时,会出现以下错误消息:

"中发生了类型为"System.Data.SqlClient.SqlException"的异常System.Data.dll,但未在用户代码中处理

附加信息:"b"附近的语法不正确

我的代码:

public void populateIngredients()
{
    string query = "select a.Name from Ingredients a" +
                   "inner join RecipeIngredient b ON a.Id = b.IngredientID" +
                   "where b.RecipeID = @RecipeID";
    using (con = new SqlConnection(connection))
    using (SqlCommand cmd = new SqlCommand(query,con))
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
        cmd.Parameters.AddWithValue("@RecipeID", listRecipes.SelectedValue);
        DataTable IngredientTable = new DataTable();
        sda.Fill(IngredientTable);    // The error points here     
        listIngredients.DisplayMember = "Name";
        listIngredients.ValueMember = "Id";
        listIngredients.DataSource = IngredientTable;
    }
}

我的数据库有三个表:

  1. Recipe(Id、Name、PrepTime、Instructions)
  2. Ingredients(Id、名称)
  3. RecipeIngredient(Id、RecipeID、IngredientID)

异常:';System.Data.SqlClient.SqlException';在System.Data.

某些单词之间缺少空格!在每个字符串常量的第一个位置放一个空格"。您的字符串查询现在是这样的:
"select a.Name from Ingredients ainner join RecipeIngredient b ON a.Id = b.IngredientID" +

并且在CCD_ 5和CCD_。