2016-04-02 21 views
-1

我有2个表。我想将一些值插入到1个表中。我正在更新的字段是ingredient_Name,Ingredient_Amount和Recipe_ID。不确定哪个连接使用以下sql代码

成分(表1)

Ingredient_Name|Ingredient_Amount|Recipe_ID 
---------------|-----------------|--------- <---- Insert into here 

配方(表2)

Recipe_Name|Recipe_ID 
yummyRecipe|----1---- <-----Recipe_ID stored here 

我使用的形式具有一个组合框,其中列出所有Recipe_Names。所以当我去插入一行到成分时,我需要从配方表中取出Recipe_ID,我已经在组合框中选择了Recipe_Name。然后在Ingredients表中使用此Recipe_ID作为ID。

我对JOIN不是很熟悉,并且不确定如何计算出使用哪个,以及是否需要使用JOIN。任何帮助或想法?

对不起,如果这太长时间啰嗦。

配方组合框代码

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True"); 
    con.Open(); 

    try 
    { 
     SqlDataAdapter da = new SqlDataAdapter("Select * FROM Recipe", con); 

     DataTable dt = new DataTable(); 

     da.Fill(dt); 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      recipeCombo.Items.Add(dt.Rows[i]["Recipe_Name"]); 
     } 

     dt.Clear(); 

    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.Message); 
    } 


    con.Close(); 
+2

为什么不将商店ID也存储在组合框中?这将避免任何问题。顺便说一下MDF文件来自Sql Server而不是MySql。或者你的标签是错误的或者你的标题 – Steve

+0

*使用.mdf本地数据库*是不相关的。使用本地数据库或远程数据库无关紧要。另外,.mdf文件是SQL Server,而不是MySQL。 –

+0

@Steve我知道如何列出组合框中的Recipe_Name,但不包含ID。你如何做到这一点? –

回答

2

您可以设置直接使用DataSource和控制您想使用DisplayMember属性中显示的字段组合框项目。加上ValueMember属性,你可以在ComboBox_SelectedIndexChanged事件写

using(SqlConnection con = new SqlConnection(....)) 
{ 
    con.Open(); 
    try 
    { 
     SqlDataAdapter da = new SqlDataAdapter("Select * FROM Recipe", con); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     recipeCombo.DataSource = dt; 
     recipeCombo.DisplayMember = "Recipe_Name"; 
     recipeCombo.ValueMember = "Recipe_ID"; 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.Message); 
    } 
} 

现在(或无处不在,你需要知道你只需要编写

if(recipeCombo.SelectedItem != null) 
{ 
    int recipeID = Convert.ToInt32(recipeCombo.SelectedValue); 
    ... and use your value for insert without any JOIN 
}  

在您需要哪个点RecipeID,(例如在SAVE按钮单击事件)添加以下INSERT

if(recipeCombo.SelectedItem == null) 
    .... error message and return.... 
else 

string sql = @"INSERT INTO Ingredient 
    (Ingredient_Name, Ingredient_Amount, Recipe_ID) 
    VALUES (@IngredientName, @IngredientFirstname, @RecipeID)"; 
using (var cmd = new SqlCommand(sql, con)) 
{ 
    cmd.Parameters.Add("@IngredientName", SqlDbType.NVarChar).Value = ingredientTxt.Text); 
    cmd.Parameters.Add("@IngredientAmount", SqlDbType.Integer).Value = Convert.ToInt32(ingredientAmount.Text); 
    cmd.Parameters.Add("@RecipeID", SqlDbType.Integer).Value = Convert.ToInt32(recipeCombo.SelectedValue); 
    cmd.ExecuteNonQuery(); 
} 

PS不要使用AddWithValue - it is a shortcut with a lot of problems -

+0

如果你说“...并使用你的价值插入没有任何JOIN”,你是什么意思? –

+0

您直接将它用作_INSERT INTO配料(Ingredient_Name,Ingredient_Amount.Recipe_ID)的值VALUE(valueForIngredient,ValueForAmount,recipeID)_ – Steve

+0

我已经使用我的插入更新了您的答案。我会在哪里把ComboBox_SelectedIndexChange事件一起放?因为我想这样保存时按下保存btn –

0
  1. 你做需要JOIN你的情况,因为你只有一表“配方”包含你需要找到“Recipe_ID”的数据。 JOIN s用于“连接”两个表。

  2. 如果“Recipe_Name”相同,则可以选择“Recipe_ID”,其中“Recipe_Name”等于组合框中选定的值,然后将新行插入“Ingredient”表中。

    INSERT INTO 成分 SELECT @Ingredient_Name,@Ingredient_Amount,Recipe_ID FROM 配方 WHERE Recipe_ID = @myComboboxSelectedValue

注:在这种情况下Recipe_ID是多余的,因为你可以将其删除您的数据库并使用Recipe_Name代替。

  • 如果“Recipe_Name”是不相同的,所以你将需要获取“Recipe_ID”的程度,并将其存储在后台代码(如果你不想把它展示给用户)并在插入查询中使用它。
  • 顺便说一句: 4.无论是使用MySQL或SQLSERVER的解决方案是一样的,所以“采用密度纤维板”的问题的标题是无关紧要的。 5.“.mdf”文件是SQLSERVER数据库的扩展。

    +0

    @Steve,它不是一个INSERT INTO与一个地方。这是一个INSERT INTO与**选择在哪里** –

    +0

    是的滑倒,评论删除 – Steve