C#我想用数据库将配料绑定到列表框中的配方中
我想在食谱中加入配料。为了做到这一点,我尝试了很多方法,但都没有奏效。 想法: 我的数据库表: 我用于添加配方的代码:C#我想用数据库将配料绑定到列表框中的配方中,c#,sql,database,listbox,C#,Sql,Database,Listbox,我想在食谱中加入配料。为了做到这一点,我尝试了很多方法,但都没有奏效。 想法: 我的数据库表: 我用于添加配方的代码: private void VoegReceptenToe() { string query = "INSERT INTO Recipe VALUES (@RecipeName, 30, 'goed mixen')"; using (connection = new SqlConnection(connectionstring))
private void VoegReceptenToe()
{
string query = "INSERT INTO Recipe VALUES (@RecipeName, 30, 'goed mixen')";
using (connection = new SqlConnection(connectionstring))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.Parameters.AddWithValue("@RecipeName", txtRecipeName.Text);
command.ExecuteScalar();
PopulateRecipes();
}
}
填充配方:
{
string query = "SELECT a.Name FROM Ingredient a " +
"INNER JOIN Recipe_Ingredient b ON a.Id = b.IngredientId " +
"WHERE b.RecipeId = @RecipeId";
// de connectionstring hoef je niet te openen en te sluiten als,
// je een using statement gebruikt, dat doet hij vanzelf.
using (connection = new SqlConnection(connectionstring))
using (SqlCommand command = new SqlCommand(query,connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
command.Parameters.AddWithValue("@RecipeId", lstRecipe.SelectedValue);
DataTable ingredientTable = new DataTable();
adapter.Fill(ingredientTable);
//Dit displayt de listbox.
lstIngredient.DisplayMember = "Name";
lstIngredient.ValueMember = "id";
lstIngredient.DataSource = ingredientTable;
}
}
您正在打开与数据库的新连接,然后再关闭现有连接 代码中发生的情况是,当执行进入method
VoegReceptenToe
时,它会打开到数据库的新连接以插入一些数据。但是,在关闭该连接之前,您将调用PopulateRecipes
方法,该方法将打开到数据库的新连接
您需要做的是拆分插入和查询操作,您可以通过使用语句将方法VoegReceptenToe
移动到之后来执行此操作:
private void VoegReceptenToe()
{
string query = "INSERT INTO Recipe VALUES (@RecipeName, 30, 'goed mixen')";
using (connection = new SqlConnection(connectionstring))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.Parameters.AddWithValue("@RecipeName", txtRecipeName.Text);
command.ExecuteScalar();
}
PopulateRecipes();
}
只要您确保配方和配料名称在其表中是唯一的(可能使用唯一索引),并且您已确保RecipeName和IngredientName在表中存在(例如,仅从列表框中选择),则以下查询可能会执行您想要的操作:
INSERT INTO Recipe_Ingredient (RecipeID, IngredientID)
VALUES (
SELECT ID FROM Recipe Where Name = @RecipeName,
SELECT ID FROM Ingredient Where Name = @IngredientName)
这将在配方配料表中插入一个条目,该条目由RecipeName的ID和IngreditName组成,只要每个条目都有一个条目
您需要将RecipeName和IngredientName的参数添加到命令中,并运行ExecuteOnQuery方法,因为INSERT不返回值
与添加新RecipeName类似,应使用ExecuteOnQuery方法运行此查询:
INSERT INTO Recipe (Name, PrepTime, Instructions) VALUES (@RecipeName, 30, 'goed mixen')
还有一种新的成分
INSERT INTO Ingredient (Name) VALUES (@Ingredient)
还要注意RePierre对代码的重新排序,以确保连接不会过早关闭。什么是“不工作”?你的代码有什么问题?不在数据库中插入配方?抛出任何异常?…感谢您的回复,我的意思是它不起作用;我尝试了以下代码:private void VoegIngredientenToe(){string query=“INSERT-INTO-component-VALUES(@IngredientName)”;using(connection=new-SqlConnection(connectionstring))using(SqlCommand=new-SqlCommand(query,connection)){connection.Open();command.Parameters.AddWithValue(“@IngreditName”,textBox1.Text);command.ExecuteScalar();PopulateRecipes();}}}@PaulF我无法向新配方添加配料。例如,我使用文本框值创建了Recipe:“nieuwtest”,并将该值插入listbox1。但我无法对配料执行此操作,并将其添加到新的“配方”。若要向配方添加配料,则需要更新配方配料表。@PaulF,我尝试了此代码,但无效:private void VoegIngredientenToe(){string query=“插入配料值(@IngredientName)”;使用(connection=new-SqlConnection(connectionstring))使用(SqlCommand-command=new-SqlCommand(query,connection)){connection.Open();command.Parameters.AddWithValue(“@IngredientName”,textBox1.Text);command.ExecuteScalar()}UpdateRecipe_配料();}谢谢,更改了它。但我仍然停留在配料部分-(我试图将查询应用到我的应用程序中,但仍然没有成功:-(.没有快速回复,因为我认为我可以找到答案(没有)…你能展示你修改过的代码吗?如果报告了错误,你会检查错误吗?谢谢你的帮助!!很抱歉我最近的反应:D!我太固执了,不得不尽快修复这个问题,以至于我没有反应:D。我所做的是,制作另一个列表框,在本例中是列表框3,用表格成分绑定它,并添加一个Multiclick事件我现在唯一想知道的是如何使用我的删除按钮进行内部联接xD。