array(41) { ["id"]=> string(7) "1110281" ["md5"]=> string(32) "1565df29b351930a51107b665dc98f59" ["title"]=> string(43) "将Emp表的主键插入Dept是一个外键" ["url"]=> string(35) "https://www.it1352.com/1147997.html" ["descript"]=> string(2066) "

我有一种情况,我想将EMP的主键放入DEPT表中

EMP emp.id emp.name emp.date

DEPT dept.id dept.name emp.id



I had a situation where I want to put Primary key of EMP into DEPT Tables are
EMP emp.id emp.name emp.date
DEPT dept.id dept.name emp.id

using (SqlConnection conn = new SqlConnection(conStr))
           {

               SqlCommand cmd = new SqlCommand();
           cmd.CommandText = "INSERT INTO EMP(empname,empdate) VALUES(@Name, @Date);"
           + "INSERT INTO DEP(deptname, empid) VALUES(@deptname, @empid);"
               cmd.CommandType = CommandType.Text;
               cmd.Connection = conn;
               cmd.Parameters.AddWithValue("@Name", txtb1.Text);
               cmd.Parameters.AddWithValue("@Date", txtb2.Text);
               cmd.Parameters.AddWithValue("@deptname", txtb3.Text);
               cmd.Parameters.AddWithValue("@empid", "select empid from emp where empid = new inserted record");





从emp中选择empid = empid = new inserted record这意味着我想选择我自己的ID现在要插入...作为可行的或任何其他方式...



"select empid from emp where empid = new inserted record" this means that i want to select id which i am going to insert now... as that posible or any other way to do so...

" ["content"]=> string(1627) "["\n

<\/p>\n

\u5982\u8bc4\u8bba\u4e2d\u6240\u8ff0\uff0c\u5b58\u50a8\u8fc7\u7a0b\u4f1a\u66f4\u597d\u505a\u6cd5\u3002\u4f46\u662f\uff0c\u60a8\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5185\u8054SQL\u6267\u884c\u6b64\u64cd\u4f5c\uff1a

<\/em>\n<\/blockquote>\n
As mentioned in the comments, a stored procedure would be a better approach. However, you can still do it with inline SQL:
\n<\/blockquote>\n
const<\/span> string<\/span> query = @\"<\/span>DECLARE @EmpID int;\n\nINSERT INTO EMP (empname, empdate) VALUES (@Name, @Date);\nSET @EmpID = Scope_Identity();\n\nINSERT INTO DEP (deptname, empid) VALUES (@deptname, @EmpID);\"<\/span>;\n\nusing<\/span> (SqlConnection conn = new<\/span> SqlConnection(conStr))\nusing<\/span> (SqlCommand cmd = new<\/span> SqlCommand(query, conn))\n{\n    cmd.Parameters.AddWithValue(\"<\/span>@Name\"<\/span>, txtb1.Text);\n    cmd.Parameters.AddWithValue(\"<\/span>@Date\"<\/span>, txtb2.Text);\n    cmd.Parameters.AddWithValue(\"<\/span>@deptname\"<\/span>, txtb3.Text);\n    \n    conn.Open();\n    cmd.ExecuteNonQuery();\n}<\/code><\/pre>\n

<\/p>\n


<\/p>"]" ["content2"]=> NULL ["pageimage"]=> NULL ["images"]=> NULL ["pages"]=> NULL ["pagetype"]=> NULL ["state"]=> string(1) "1" ["spidertime"]=> string(10) "1650112486" ["contenttime"]=> string(10) "1674954764" ["readtime"]=> string(1) "1" ["tag"]=> string(27) "ASP.NET|C#5|SQL-server-2012" ["downpath"]=> NULL ["cate1"]=> string(12) "技术问答" ["cate2"]=> NULL ["cate3"]=> NULL ["cate4"]=> NULL ["attr1"]=> string(18) "其他开发语言" ["attr2"]=> NULL ["attr3"]=> NULL ["attr4"]=> NULL ["attr5"]=> NULL ["pushed"]=> string(1) "1" ["visitnum"]=> string(3) "105" ["updatetime"]=> string(1) "0" ["originalurl"]=> NULL ["edit_tag"]=> string(1) "0" ["edit_pageimage"]=> string(1) "0" ["sitename"]=> string(6) "it1352" ["originalsite"]=> NULL ["category1"]=> NULL ["category2"]=> NULL ["category3"]=> NULL ["category4"]=> NULL ["category5"]=> NULL ["is_edit"]=> string(1) "0" ["is_html"]=> string(1) "0" }