我有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();
为什么不将商店ID也存储在组合框中?这将避免任何问题。顺便说一下MDF文件来自Sql Server而不是MySql。或者你的标签是错误的或者你的标题 – Steve
*使用.mdf本地数据库*是不相关的。使用本地数据库或远程数据库无关紧要。另外,.mdf文件是SQL Server,而不是MySQL。 –
@Steve我知道如何列出组合框中的Recipe_Name,但不包含ID。你如何做到这一点? –