我运行代码时收到此错误消息。它说“类型'System.Data.SqlClient.SqlException'的异常在System.Data.dll中发生但未在用户代码中处理附加信息:'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();
}
}
}
答案 0 :(得分:2)
您获得的查询字符串在换行符周围缺少空格:当您连接时,例如" SELECT a.Name FROM Ingredient a" + "INNER JOIN RecipeIngredient b ON a.Id = b.IngredientID"
,a
和INNER 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时具有有效的语法,并且在屏幕上也可以很好地读取。