不确定与以下sql代码一起使用的连接

时间:2016-04-02 21:09:36

标签: sql-server visual-studio

我有两张桌子。我想在1个表中插入一些值。我要更新的字段是ingredient_Name,Ingredient_Amount和Recipe_ID。

成分(表1)

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

食谱(表2)

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

我正在使用的表单有一个comboBox,列出了所有Recipe_Names。因此,当我在成分中插入一行时,我需要从Recipe表中获取Recipe_ID,其中我在comboBox中选择了Recipe_Name。然后使用此Recipe_ID作为Ingredients表中的ID。

我对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 个答案:

答案 0 :(得分:2)

您可以直接使用DataSource设置ComboBox项目,并使用DisplayMember属性控制要显示的字段。与ValueMember属性一起,您可以编写

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);
    }
}

现在在ComboBox_SelectedIndexChanged事件中(或者您需要知道RecipeID的任何地方,您只需编写

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

无论您需要什么点,(例如在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 -

答案 1 :(得分:0)

  1. 你的需要一个 JOIN ,因为你只有一个表&#34; Recipe&#34;包含您需要查找的数据&#34; Recipe_ID&#34;。 JOIN 用于&#34;加入&#34;两张桌子。

  2. 如果&#34; Recipe_Name&#34;是相同的,您可以选择&#34; Recipe_ID&#34;其中&#34; Recipe_Name&#34;等于组合框中的选定值,然后将新行插入&#34;成分&#34;表

    INSERT INTO 成分 SELECT @Ingredient_Name,@ Ignredient_Amount,Recipe_ID FROM 食谱 WHERE Recipe_ID = @myComboboxSelectedValue

  3. 注意:在这种情况下, Recipe_ID 是多余的,因为您可以将其从数据库中删除并改为使用 Recipe_Name

    1. 如果&#34; Recipe_Name&#34;是不一样的,所以你需要获取&#34; Recipe_ID&#34;使用它并将其存储在代码隐藏中(如果您不想将其显示给用户)并在插入查询中使用它。
    2. 顺便说一下: 4.无论是使用MYSQL还是SQLSERVER,解决方案都是相同的,所以&#34;使用.mdf&#34;在问题的标题是无关紧要的。 5.&#34; .mdf&#34;文件是SQLSERVER数据库的扩展。