嘿伙计们,我的手上有点问题,
使用VB.Net;我正在尝试 将一个表中的一个记录中的字段 复制到另一个表中另一个记录中的 不同字段 ,同时还 使用用户将插入到我的一个VB表单上的文本框中的值插入新记录 。
使用的两个表被称为' Staff'和'角色'他们的每一个内容都在下面:
职员:
稍后会更新!
作用:
稍后会更新!
当用户在我的表单上输入了一些值并按下按钮时,将触发以下代码
Private Sub AddSMButton_Click(sender As Object, e As EventArgs) Handles AddSMButton.Click
If DbConnect() Then
Dim SQLCmd As New OleDbCommand
If StaffID = -1 Then
With SQLCmd
.Connection = cn
.CommandText = "Insert into Staff (StaffMemberFirstName, StaffMemberLastName, StaffMemberDOB, StaffMemberGender, StaffMemberContactNumber, StaffMemberEmail, StaffMemberPostcode, StaffMemberHomeAddress, StaffMemberHoursWorked, UnitID, RoleID) " &
"Select @StaffMemberFirstName, @StaffMemberLastName, @StaffMemberDOB, @StaffMemberGender, @StaffMemberContactNumber, @StaffMemberEmail, @StaffMemberPostcode, @StaffMemberHomeAddress, @StaffMemberHoursWorked, @UnitID, @RoleID"
.Parameters.AddWithValue("@StaffMemberFirstName", AddSM_FNInput.Text)
.Parameters.AddWithValue("@StaffMemberLastName", AddSM_LNInput.Text)
.Parameters.AddWithValue("@StaffMemberDOB", AddDOB_DTP.Value.Date)
.Parameters.AddWithValue("@StaffMemberGender", AddSM_GInput.Text)
.Parameters.AddWithValue("@StaffMemberContactNumber", AddSM_CNInput.Text)
.Parameters.AddWithValue("@StaffMemberEmail", AddSM_EInput.Text)
.Parameters.AddWithValue("@StaffMemberPostcode", AddSM_PInput.Text)
.Parameters.AddWithValue("@StaffMemberHomeAddress", AddSM_AInput.Text)
.Parameters.AddWithValue("@StaffMemberHoursWorked", AddHW_TB.Text)
.Parameters.AddWithValue("@UnitID", AddU_CB.SelectedItem.Identifier)
.Parameters.AddWithValue("@RoleID", AddR_CB.SelectedItem.Identifier)
.ExecuteNonQuery()
.CommandText = "Select @@Identity"
StaffID = .ExecuteScalar
AddSalary(StaffID)
End With
MessageBox.Show("The new record has been inserted!")
AddSM_FNInput.Text = ""
AddSM_LNInput.Text = ""
AddSM_GInput.Text = ""
AddSM_CNInput.Text = ""
AddSM_EInput.Text = ""
AddSM_PInput.Text = ""
AddSM_AInput.Text = ""
AddHW_TB.Text = ""
End If
cn.Close()
StaffID = -1
End If
End Sub
上面的代码能够正确添加具有指定值的新记录。
但 没有将薪水 添加到新记录中,我不知道为什么?
以下代码是AddSalary过程
Private Sub AddSalary(StaffID)
If DbConnect() Then
Dim SQLCmd As New OleDbCommand
With SQLCmd
.Connection = cn
.CommandText = "Insert into Staff (StaffMemberWages) " &
"Select Salary " &
"From Role " &
"Where Role.RoleID = @RoleID and Staff.StaffID = @StaffID"
.Parameters.AddWithValue("@RoleID", AddR_CB.SelectedItem.Identifier)
.Parameters.AddWithValue("@StaffID", StaffID)
End With
End If
End Sub
有几个原因可以解释为什么我认为它不起作用但不确定它们是否正确。
总而言之,我正在尝试创建一个SQL语句,这将允许我在“工作人员”中插入一条新记录。同时也将“新角色”中的一个值复制到“新角色”中。表
非常感谢任何帮助,
感谢您阅读并度过美好的一天!
答案 0 :(得分:1)
你根本不需要AddSalary
。您已拥有RoleID
,因此请Salary
中的Role
作为Insert
的一部分。
Private Sub AddSMButton_Click(sender As Object, e As EventArgs) Handles AddSMButton.Click
If DbConnect() Then
Dim SQLCmd As New OleDbCommand
If StaffID = -1 Then
With SQLCmd
.Connection = cn
.CommandText = "Insert into Staff (StaffMemberFirstName, StaffMemberLastName, StaffMemberDOB, StaffMemberGender, StaffMemberContactNumber, StaffMemberEmail, StaffMemberPostcode, StaffMemberHomeAddress, StaffMemberHoursWorked, UnitID, RoleID, StaffMemberWages) " & //Add Salary to the fields being inserted
"Select @StaffMemberFirstName, @StaffMemberLastName, @StaffMemberDOB, @StaffMemberGender, @StaffMemberContactNumber, @StaffMemberEmail, @StaffMemberPostcode, @StaffMemberHomeAddress, @StaffMemberHoursWorked, @UnitID, " &
"@RoleID, Role.Salary FROM Role WHERE Role.RoleID = @RoleID" //I added this line
.Parameters.AddWithValue("@StaffMemberFirstName", AddSM_FNInput.Text)
.Parameters.AddWithValue("@StaffMemberLastName", AddSM_LNInput.Text)
.Parameters.AddWithValue("@StaffMemberDOB", AddDOB_DTP.Value.Date)
.Parameters.AddWithValue("@StaffMemberGender", AddSM_GInput.Text)
.Parameters.AddWithValue("@StaffMemberContactNumber", AddSM_CNInput.Text)
.Parameters.AddWithValue("@StaffMemberEmail", AddSM_EInput.Text)
.Parameters.AddWithValue("@StaffMemberPostcode", AddSM_PInput.Text)
.Parameters.AddWithValue("@StaffMemberHomeAddress", AddSM_AInput.Text)
.Parameters.AddWithValue("@StaffMemberHoursWorked", AddHW_TB.Text)
.Parameters.AddWithValue("@UnitID", AddU_CB.SelectedItem.Identifier)
.Parameters.AddWithValue("@RoleID", AddR_CB.SelectedItem.Identifier)
.ExecuteNonQuery()
.CommandText = "Select @@Identity"
StaffID = .ExecuteScalar
// AddSalary(StaffID) Don't need this function anymore
End With
MessageBox.Show("The new record has been inserted!")
AddSM_FNInput.Text = ""
AddSM_LNInput.Text = ""
AddSM_GInput.Text = ""
AddSM_CNInput.Text = ""
AddSM_EInput.Text = ""
AddSM_PInput.Text = ""
AddSM_AInput.Text = ""
AddHW_TB.Text = ""
End If
cn.Close()
StaffID = -1
End If
End Sub
这是单独的SQL查询:
Insert into Staff
(StaffMemberFirstName
, StaffMemberLastName
, StaffMemberDOB
, StaffMemberGender
, StaffMemberContactNumber
, StaffMemberEmail
, StaffMemberPostcode
, StaffMemberHomeAddress
, StaffMemberHoursWorked
, UnitID
, RoleID
, StaffMemberWages)
Select
@StaffMemberFirstName
, @StaffMemberLastName
, @StaffMemberDOB
, @StaffMemberGender
, @StaffMemberContactNumber
, @StaffMemberEmail
, @StaffMemberPostcode
, @StaffMemberHomeAddress
, @StaffMemberHoursWorked
, @UnitID
, @RoleID
, Role.Salary
FROM Role
WHERE Role.RoleID = @RoleID