【问题标题】:An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Incorrect syntax near 'b'System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常“b”附近的语法不正确
【发布时间】:2018-05-07 06:57:14
【问题描述】:

我在运行代码时收到此错误消息。它说 “System.Data.dll 中发生了 'System.Data.SqlClient.SqlException' 类型的异常,但未在用户代码中处理其他信息:'b' 附近的语法不正确。” 但我不知道如何解决。实际上我引用了某人的代码,它与他们的代码相同,但我仍然出错。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace myCookbook
{
    public partial class Form1 : Form
    {
        SqlConnection connection;
        string connectionString;
        public Form1()
        {
            InitializeComponent();
            connectionString = ConfigurationManager.ConnectionStrings["myCookbook.Properties.Settings.cookBookDatabaseconnectionString"].ConnectionString;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            InsertResipi();
        }

        private void InsertResipi()
        {
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM Recipe", connection))
            {
                DataTable RecipeTable = new DataTable();
                myAdapter.Fill(RecipeTable);
                listRecipes.DisplayMember = "ResepiName";
                listRecipes.ValueMember = "Id";
                listRecipes.DataSource = RecipeTable;
            }
        }

        private void InsertIngredient()
        {
            string myQuery = " SELECT a.IngredientName FROM Ingredient a" +
            "INNER JOIN ResipiIngredient b ON a.Id = b.IngredientId" +
            "WHERE b.ResipiId = @ResipiId";

            using (connection = new SqlConnection(connectionString))
            using (SqlCommand myCommand  = new SqlCommand(myQuery, connection))
            using (SqlDataAdapter adapter = new SqlDataAdapter(myCommand))
            {
                myCommand.Parameters.AddWithValue("@ResipiId", listRecipes.SelectedValue);
                DataTable IngredientTable = new DataTable();
                adapter.Fill(IngredientTable); // the error message highlight this line
                listIngredient.DisplayMember = "IngredientName";
                listIngredient.ValueMember = "Id";
                listIngredient.DataSource = IngredientTable;
            }
        }

        private void listRecipes_SelectedIndexChanged(object sender, EventArgs e)
        {
            // MessageBox.Show(listRecipes.SelectedValue.ToString()); //show id
            InsertIngredient();
        }
    }
}

这是我的参考资料

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace databasecookbook
{
    public partial class Form1 : Form
    {
        string connectiondenganawak;
        SqlConnection connectionhati;

        public Form1()
        {
            InitializeComponent();
            connectiondenganawak =     ConfigurationManager.ConnectionStrings
    ["databasecookbook.Properties.Settings.cookbookConnectionString"].
    ConnectionString;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            isirecipe();
        }

        void isirecipe()
        {
            using (connectionhati = new SqlConnection(connectiondenganawak))
            using (SqlDataAdapter adapterawak = new SqlDataAdapter("SELECT * FROM Recipe", connectionhati))
            {
                DataTable RecipeTable = new DataTable();
                adapterawak.Fill(RecipeTable);
                listBoxRecipe.DisplayMember = "Name";
                listBoxRecipe.ValueMember = "ID";
                listBoxRecipe.DataSource = RecipeTable;
            }
        }

        void isiingredient()
        {
            string querycinta = " SELECT a.Name FROM Ingredient a" +
            "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientID" +
            "WHERE b.RecipeID = @RecipeID";

             using (connectionhati = new SqlConnection(connectiondenganawak))
            using (SqlCommand commandawak =new SqlCommand(querycinta,connectionhati))
            using (SqlDataAdapter adapterawak = new SqlDataAdapter(commandawak))
            {
                commandawak.Parameters.AddWithValue("@RecipeID", listBoxRecipe.SelectedValue);
                DataTable IngredientTable = new DataTable();
                adapterawak.Fill(IngredientTable);
                listBoxIngredients.DisplayMember = "Name";
                listBoxIngredients.ValueMember = "ID";
                listBoxIngredients.DataSource = IngredientTable;
            }
        }

        private void listBoxRecipe_SelectedIndexChanged(object sender, EventArgs e)
        {
            isiingredient();
        }
    }
}

【问题讨论】:

  • 我敢打赌 SQL 查询是不正确的 - 当您获取查询并直接在 SQL Server 中运行它时会发生什么?
  • 在调试器中查看myQuery 包含的内容:“SELECT a.IngredientName FROM Ingredient aINNER JOIN ResipiIngredient b ON a.Id = b.IngredientIdWHERE b.ResipiId = @ResipiId”。您需要在单词周围添加空格。
  • @DourHighArch 在单词周围添加空格是什么意思?
  • @5c625b7c 你的意思是复制查询并在 sql server 上运行它吗?我所做的是复制查询并在 c# 的 sql 查询中运行它,我在“+”附近收到错误语法错误。对不起,我只是一个初学者无法理解你的意思。

标签: c# sql


【解决方案1】:

您得到的查询字符串在换行符周围缺少空格:当您连接时,例如 " SELECT a.Name FROM Ingredient a" + "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientID"aINNER JOIN 之间没有空格,因此查询语法不正确。

C# 通过逐字字符串文字 为问题提供内置解决方案,即以@ 开头的文字。这些文字可以包含换行符:

string myQuery =@"
    SELECT a.IngredientName FROM Ingredient a
    INNER JOIN ResipiIngredient b ON a.Id = b.IngredientId
    WHERE b.ResipiId = @ResipiId
";

当您将这个查询传递给 SQL Server 时,它具有有效的语法,并且在屏幕上也能很好地阅读。

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多