我想添加配料到配方中。爲了做到這一點,我嘗試過很多事情,但這並不令人傷心。 理念:http://prntscr.com/dw8lom 我的數據庫表:http://prntscr.com/dw8ms7C#我想要將配料粘貼到帶有數據庫的列表框中的配方
我的代碼我用來添加配方:
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;
}
}
「不起作用」是什麼意思?你的代碼有什麼問題?不在數據庫中插入配方?拋出任何異常?... – Pikoh
感謝您的回覆, 由於它沒有工作,我的意思是;我已經嘗試了以下代碼: private void VoegIngredientenToe() { string query =「INSERT INTO Ingredient VALUES(@IredredName)」; (連接字符串) 使用(SqlCommand命令=新的SqlCommand(查詢,連接)) connection.Open(); command.Parameters.AddWithValue(「@ IngredientName」,textBox1.Text); 命令。的ExecuteScalar(); PopulateRecipes(); } } – overflowit
@PaulF我不能將配料添加到新配方中。例如食譜:「nieuwtest」我已經通過使用文本框的值創建並將該值插入到listbox1。但我無法爲成分做到這一點,並將其添加到新的「配方」中。 – overflowit