【发布时间】:2020-01-24 15:31:09
【问题描述】:
我有一个包含 3 个表 Identification、Identification_group 和 Group 的数据库。
Identification_group 表有 2 个外键列,Identificaiton_id 和 Group_id。
在我的表单中,您可以选择组号,它将正确的 Group_id 返回到Identification_group 表。
是否可以编写一个 SQL 查询,使用最后一个 Identification_id 和 Group_id(来自我表单中的选择字段)并将其写入 Identification_group 表?
string sqlquery1 = "Insert into [dbo].[identification_group] (fk_group_id,fk_identification_id values (@fk_group_id,@fk_identification_id;";
SqlCommand schreiben = new SqlCommand(sqlquery1, myConnection);
myConnection.Open();
schreiben.Parameters.AddWithValue("@fk_identification_id", intidentification_id);
schreiben.Parameters.AddWithValue("@fk_group_id", Wil_Jls_idComboBox.SelectedValue);
schreiben.CommandText = sqlquery;
schreiben.ExecuteNonQuery();
myConnection.Close();
抱歉格式错误,第一次在这里发帖。
提前谢谢你
【问题讨论】:
-
您只需要使用 schreiben.ExecuteNonQuery() 执行命令
-
忘记添加了,现在更新我的代码。制作问题的行是 "schreiben.Parameters.AddWithValue("@fk_identification_id", intidentification_id);"我想我需要使用“lastSelectedId”之类的东西,但我似乎无法让它发挥作用。
-
从
schreiben.Parameters.AddWithValue("@fk_identification_id", intidentification_id);中删除@符号-您只需要SQL 查询语句中的@符号,而不需要在实际的C# 代码中。您的 SQL 语句中也没有右括号
标签: c# sql foreign-keys sqlcommand