如何从表单插入多个表? VB

时间:2014-04-27 03:51:31

标签: sql-server vb.net

我有3个表Check_InCheck_OutPatient_Detail,我想用vb.net表单立即插入它们。我已经为这些表格创建了关系。它没有显示任何错误,也不起作用。我试图找到解决方案2天我没有更多我亲爱的去做.plz帮助

 If DialogResult = Windows.Forms.DialogResult.OK Then
            '' Try
            cmd = New SqlCommand
            Dim cmd2 As New SqlCommand
            Dim cmd3 As New SqlCommand
            cmd2.Connection = cn
            cmd3.Connection = cn
            cmd.Connection = cn
            'cmd.CommandText = "INSERT INTO Table_PatientDetail (Patient_ID , Patient_Name , Age,Sex , Phone_Number , Address , Service , Transfer , Patient_Result ) VALUE (" & .PatientIDPatientInformation.Text & "','" & .PatientNamePatientInformation.Text & "','" & .AgePatientInformation.Text & "','" & .ComboBox1.Text & "','" & .PhoneNumPatientInformation.Text & "','" & .AddressPatientInformation.Text & "','" & .Cboservice.SelectedValue & "','" & .TransferPatientInformation.Text & "','" & .TransferPatientInformation.Text & "')"
            cmd.CommandText = "INSERT INTO Table_PatientDetail (Patient_ID , Patient_Name , Age,Sex , Phone_Number , Address, Check_In_ID, Check_Out_ID , Service , Transfer , Patient_Result ) VALUES (@Patient_ID , @Patient_Name , @Age,Sex , @Phone_Number , @Address, @Check_In_ID , Check_Out_ID@ , @Service , @Transfer , @Patient_Result)"

            cmd.Parameters.AddWithValue("@Patient_ID", .PatientIDPatientInformation.Text)
            cmd.Parameters.AddWithValue("@Patient_Name", .PatientNamePatientInformation.Text)
            cmd.Parameters.AddWithValue("@Age", .AgePatientInformation.Text)
            cmd.Parameters.AddWithValue("@Sex", .ComboBox1.Text)
            cmd.Parameters.AddWithValue("@Phone_Number", .PhoneNumPatientInformation.Text)
            cmd.Parameters.AddWithValue("@Address", .AddressPatientInformation.Text)
            cmd.Parameters.AddWithValue("@Service", .Cboservice.SelectedValue)
            cmd.Parameters.AddWithValue("@Transfer", .TransferPatientInformation.Text)
            cmd.Parameters.AddWithValue("@Patient_Result", .ComboBox3.Text)

            cmd2.CommandText = " INSERT INTO Table Table_Check_in ( Check_In_ID , Check_In_Date , Illness_ID) VALUES ( @Check_In_ID , @Check_In_Date ,@CI_Illness_ID )"
            cmd.Parameters.AddWithValue("@Check_In_ID", .CIID.Text)
            cmd.Parameters.AddWithValue("@Check_In_Date", .CheckinDateTimePicker1.Text)
            cmd.Parameters.AddWithValue("@CI_Illness_ID", .CheckInillnessComboBox.SelectedValue)

            cmd3.CommandText = " INSERT INTO Table Table_Check_Out ( Check_Out_ID , Check_Out_Date , Illness_ID) VALUES ( @Check_Out_ID , @Check_Out_Date ,@CO_Illness_ID )"
            cmd.Parameters.AddWithValue("@Check_Out_ID", .COID.Text)
            cmd.Parameters.AddWithValue("@Check_Out_Date", .CheckoutDateTimePicker2.Text)
            cmd.Parameters.AddWithValue("@CO_Illness_ID", .CheckOutillnessComboBox.SelectedValue)

            cmd.ExecuteNonQuery()
            cmd2.ExecuteNonQuery()
            cmd3.ExecuteNonQuery()

           Catch ex As Exception

            MsgBox(ex.ToString)

            End Try
 End if

1 个答案:

答案 0 :(得分:0)

在第二个和第三个插入命令中,您要添加到第一个命令的参数的一个快速观察,不应该是cmd2和cmd3吗?

cmd2.CommandText = " INSERT INTO Table Table_Check_in ( Check_In_ID , Check_In_Date , Illness_ID) VALUES ( @Check_In_ID , @Check_In_Date ,@CI_Illness_ID )"
            cmd2.Parameters.AddWithValue("@Check_In_ID", .CIID.Text)
            cmd2.Parameters.AddWithValue("@Check_In_Date", .CheckinDateTimePicker1.Text)
            cmd2.Parameters.AddWithValue("@CI_Illness_ID", .CheckInillnessComboBox.SelectedValue)

            cmd3.CommandText = " INSERT INTO Table Table_Check_Out ( Check_Out_ID , Check_Out_Date , Illness_ID) VALUES ( @Check_Out_ID , @Check_Out_Date ,@CO_Illness_ID )"
            cmd3.Parameters.AddWithValue("@Check_Out_ID", .COID.Text)
            cmd3.Parameters.AddWithValue("@Check_Out_Date", .CheckoutDateTimePicker2.Text)
            cmd3.Parameters.AddWithValue("@CO_Illness_ID", .CheckOutillnessComboBox.SelectedValue)