我有两张表,一张包含姓名,一张包含费率和与每个姓名对应的其他数据。在表 A 中插入一个新名称后,我想获取新自动生成的 PK,以便现在用于插入到我的另一个表 B 中。
我怎样才能做到这一点?我在网上阅读过,scope_identity
但我不知道如何使用它。
这是我到目前为止所拥有的:
SqlConnection con = new SqlConnection(pubvar.x);
SqlCommand command = con.CreateCommand();
command.CommandText ="Insert into A values('" +Name + "')";
SqlCommand command2 = con.CreateCommand();
command2.CommandText = "Insert into B values(....)";
SELECT SCOPE_IDENTITY();
con.Open();
command.ExecuteNonQuery();
con.Close();