C# System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常“b”附近语法不正确
我在运行代码时收到此错误消息。它说System.Data.dll中发生了“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理其他信息:“b”附近的语法不正确。但是我不知道怎么解决它。实际上,我引用了某人的代码,它与他们的代码完全相同,但我仍然得到了错误 这是我的代码:C# System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常“b”附近语法不正确,c#,sql,C#,Sql,我在运行代码时收到此错误消息。它说System.Data.dll中发生了“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理其他信息:“b”附近的语法不正确。但是我不知道怎么解决它。实际上,我引用了某人的代码,它与他们的代码完全相同,但我仍然得到了错误 这是我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System
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();
}
}
}
您得到的查询字符串在换行符周围缺少空格:当您在a.Id=b.IngredientID上连接,例如,从成分a+内部联接RecipeCredit b中选择a.Name时,a和内部联接之间没有空格,因此查询语法不正确 C通过逐字字符串文本(即以@开头的文本)为问题提供内置解决方案。这些文字可以包括换行符:
string myQuery =@"
SELECT a.IngredientName FROM Ingredient a
INNER JOIN ResipiIngredient b ON a.Id = b.IngredientId
WHERE b.ResipiId = @ResipiId
";
当您将此查询传递给SQL Server时,它具有有效的语法,而且它在屏幕上的显示效果也很好。我打赌SQL查询是不正确的-当您直接在SQL Server中执行查询时会发生什么?在调试器中查看myQuery包含的内容:从成分ainer中选择a.IngredientName加入成分RespiComponent b on a.Id=b.IngRedientWhere b.RespiId=@RespiId。您需要在单词周围添加空格。@DourHighArch在单词周围添加空格是什么意思?@5c625b7c您的意思是复制查询并在sql server上运行它吗?我所做的是复制查询并在c中的sql查询中运行它,我在“+”附近得到了错误的语法。对不起,我只是个初学者,不明白你说的是什么意思。