我有四张桌子:
M_Division
Division_ID(primary key) Division
1 abc
2 xyz
M_Designation
Designation_ID(primary key) Designation
1 scientist
2 doctor
M_emp personal
empid(primary key) Divsion_ID(foreign key) Designation_ID(foreign key) emp_name
1 1 1 mike
2 2 2 justin
M_temp
empid(foreign key) Divsion_ID(foreign key) Designation_ID(foreign key) date purpose
1 1 1 12-06-2013 xyz
2 2 2 12-06-2013 abc
我想将外键 ID 插入到 M_temp 表中......如果我给出这样的查询,它会给我外键约束错误......
Insert into M_temp(EmpID,DivisionID,DesigID,date,purpose,reason,created_date) Select(EmpID,DivisionID,DesigID from M_emp_personal where Empid=@empid",conn);
目的,日期是我的 ASP.NET 中的表单字段,它们需要与 DivID DesigID 和 EmpID 一起插入 M_temp ......这就是我面临的问题......我不知道该怎么做有人可以帮帮我吗?
这是 C# 代码
protected void btnSubmit_Click2(object sender, EventArgs e)
{
string RelaseDate = Calendar1.SelectedDate.Date.ToString();
SqlCommand cmd = new SqlCommand("Insert into T_TADA_tempform(EmpID,DivisionID,DesigID,date,purpose,reason,created_date) values(@EmpID,@DivisionID,@DesigID,@GPFNo,@date,@purpose,@reason), conn);
cmd.Parameters.AddWithValue("@EmpID", ddlname.SelectedValue);
cmd.Parameters.AddWithValue("@DivisionID", divisionID);
cmd.Parameters.AddWithValue("@DesigID", lbldiv.Text);
cmd.Parameters.AddWithValue("date", RelaseDate);
cmd.Parameters.AddWithValue("@purpose", ddlpurpose.SelectedValue);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
int cnt = cmd.ExecuteNonQuery();
conn.Close();
if (cnt == 1)
{
Response.Redirect("");
}
else
Response.Write("Form has not been submitted,Please Try again!");
}
}