【发布时间】:2017-09-18 11:19:39
【问题描述】:
如何将数据插入外键表?
我在数据库Addemployees 和Normal_L 中有两个表包含一个关系,即ID。我想将数据插入Normal_L 表中,其中ID 必须与Addemployees 表保持相关。
Dim cmd As New System.Data.SqlClient.SqlCommand
Dim RA As Integer
Try
cn.Open()
cmd.CommandType = System.Data.CommandType.Text
If Me.RadioButton1.Checked Then
cmd = New SqlCommand("INSERT into Normal_L (fromD,toD,DScrip) VALUES ('" & DateTimePicker2.Text & "','" & DateTimePicker1.Text & "','" & TextBox5.Text & "') where ID ='" & ComboBox1.Text & "' ", cn)
cmd.Connection = cn
RA = cmd.ExecuteNonQuery()
MessageBox.Show("Process successful!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
cn.Close()
End If
Catch
MessageBox.Show("Error!", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
End Sub
我希望你能帮我解决这个问题
【问题讨论】:
-
插入 ... 不存在的地方。使用外键,如果 Addemployees 中不存在外键,则您不应该能够插入 Normal_L。请注意,如果 Normal_L.ID 上的外键约束不匹配,则会引发错误
-
你介意告诉我如何完成这项工作
标签: sql-server vb.net