C# 与SQL Server数据库相关的语法错误

C# 与SQL Server数据库相关的语法错误,c#,database,winforms,visual-studio-2017,C#,Database,Winforms,Visual Studio 2017,我正在努力学习在VisualStudio上使用数据库。我从YouTube上的一个人那里复制了以下代码,没有错误,但我仍然得到了一个错误 我对以下代码有问题: public partial class FormMain : Form { SqlConnection connection; string ConnectionString; public FormMain() { InitializeComponent(); Con

我正在努力学习在VisualStudio上使用数据库。我从YouTube上的一个人那里复制了以下代码,没有错误,但我仍然得到了一个错误

我对以下代码有问题:

public partial class FormMain : Form
{
    SqlConnection connection;

    string ConnectionString;

    public FormMain()
    {
        InitializeComponent();

        ConnectionString = ConfigurationManager.ConnectionStrings["denis_project.Properties.Settings.denisdatabaseConnectionString"].ConnectionString;
    }

    private void FormMain_Load(object sender, EventArgs e)
    {
        PopulateRecipes();
    }

    private void PopulateRecipes()
    {
        using (connection = new SqlConnection(ConnectionString))
        using(SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Recipe", connection))
        {             
            DataTable recipeTable = new DataTable();
            adapter.Fill(recipeTable);

            lstRecipes.DisplayMember = "Name";
            lstRecipes.ValueMember = "Id";
            lstRecipes.DataSource = recipeTable;
        }
    }

    private void PopulateIngredients()
    {
        string query = "SELECT a.Name FROM Ingredient a " +
            "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientId" +
            "WHERE b.RecipeId = @RecipeId";

        using (connection = new SqlConnection(ConnectionString))
        using (SqlCommand command = new SqlCommand(query, connection))
        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        {
            command.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue);

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

            lstIngredients.DisplayMember = "Name";
            lstIngredients.ValueMember = "Id";
            lstIngredients.DataSource = ingredientTable;
        }
    }

    private void lstRecipes_SelectedIndexChanged(object sender, EventArgs e)
    {
        PopulateIngredients();
    }
}
问题是此代码在这一行上抛出错误:

 adapter.Fill(ingredientTable);
错误显示:

System.Data.SqlClient.SqlException:“b附近的语法不正确。”

我试图在我的代码中查找任何错误,但没有发现任何类型的错误

也许这会有所帮助,我从
SQLQuery1.sql
复制了它:

 SELECT * FROM recipe

 SELECT * FROM Ingredient

 SELECT a.Name 
 FROM Ingredient a
 INNER JOIN RecipeIngredient b ON a.Id = b.Ingredientid

可能您应该在WHERE子句之前添加空格:

string query = "SELECT a.Name FROM Ingredient a " +
            "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientId" +
            " WHERE b.RecipeId = @RecipeId";

我不知道你是不是这个意思,但你可以这么说:是的,但一般来说,如果有人问你更多的信息,最好把它添加到问题中,而不是评论中。另外,我没有看到代码的查询部分,不知道为什么没有看到,但我没有看到代码的其余部分,因此我试图找到包含
b
的错误源。如何在注释中发布代码-_-一般来说,你不应该——你应该把它加入你的问题中。阅读注释中的代码非常困难。@Touareg删除的答案看起来是正确的,但不确定删除的原因。连接字符串时缺少一个空格,该空格变成“…b.IngredientIdWHERE b.RecipeId…”。您应该考虑使用逐字串文字(带有@前缀),这样可以创建多行字符串文字并避免此类问题。