2017-01-16 11 views
-1

レシピに成分を追加したい。私はそれをするために多くのことを試みましたが、それは悲しいことには働きませんでした。 アイデア: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; 
     } 
    } 
+1

「うまくいかない」とはどういう意味ですか?あなたのコードの問題は何ですか?レシピをデータベースに挿入しませんか?例外をスローしますか? – Pikoh

+0

返信ありがとうございました。 それはうまくいきませんでした。私は次のコードを試しました: プライベートvoid VoegIngredientenToe() { string query = "INSERT INTO Ingredient VALUES(@IngredientName)"; (SqlCommandコマンド=新しいSqlCommand(クエリ、接続)) { connection.Open();を使用して、 を使用して(接続=新しいSqlConnection(接続文字列)) を使用します。 command.Parameters.AddWithValue( "@ IngredientName"、textBox1.Text); コマンドExecuteScalar(); PopulateRecipes(); } } – overflowit

+0

@PaulF新しいレシピに成分を追加することはできません。例えば、レシピ: "nieuwtest"私はテキストボックスの値を使って作成し、その値をlistbox1に挿入します。しかし、私は材料のためにそれを行うことはできませんし、新しい "レシピ"に追加します。 – overflowit

答えて

0

は、データベースへの新しい接続を開いています既存のものを閉じる前に。

コードでは、実行時にメソッドVoegReceptenToeが入力されると、データベースへの新しい接続が開き、データが挿入されます。ただし、その接続を閉じる前に、PopulateRecipesメソッドを呼び出してデータベースへの新しい接続を開きます。

は何がしなければならないことは、挿入とクエリの操作を分割することであり、あなたが方法を移動することで、これを行うことができ VoegReceptenToe using文の後:限り、あなたはそのレシピ&成分名を確保するよう

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(); 
} 
+0

ありがとう、それを変更しました。しかし、私はまだ成分の部分に立ち往生しています。 :-( – overflowit

0

&テーブル内にRecipeName & IngredientNameが存在することを確認した場合(リストボックスからのみ選択するなど)、次のクエリは必要な処理を実行できます。

INSERT INTO Recipe_Ingredient (RecipeID, IngredientID) 
VALUES (
SELECT ID FROM Recipe Where Name = @RecipeName, 
SELECT ID FROM Ingredient Where Name = @IngredientName) 

これは、Recipe_Ingredientテーブルに、それぞれ1つのエントリが存在する限り、RecipeName & IngredientNameのIDで構成されるエントリを挿入します。

RecipeName & IngredientNameの両方のパラメータをコマンドに追加し、INSERTが値を返さないためExecuteNonQueryメソッドを実行する必要があります。

同様ExecuteNonQueryメソッドは、このクエリを実行するために使用されるべき新しいRecipeNameを追加する:

INSERT INTO Recipe (Name, PrepTime, Instructions) VALUES (@RecipeName, 30, 'goed mixen') 

と新しい成分

INSERT INTO Ingredient (Name) VALUES (@Ingredient) 

ためにも、あなたのコードのRePierreの並べ替えをメモを取ります接続が時期尚早に閉じられていないことを確認します。

+0

私は自分のアプリケーションにクエリを適用しようとしましたが、まだそれを行うことができませんでした:-(。私はそれを把握することができたと思ったので、速く返信しませんでした。 – overflowit

+0

できますか – PaulF

+0

あなたの助けてくれてありがとう!!私の後半の反応で申し訳ありません:D!私は私がそうしたようにできるだけ修正しなければならないほど頑固でした。反応:D。私がやったことは、別のリストボックスを作って、リストボックス3を作成し、これをテーブルの成分と結びつけて、マルチキックケイベントを追加することです!私が今考えている唯一のことは、 xD。 – overflowit

関連する問題