如果Select查询未返回任何行,请在vb.net中执行insert query

时间:2018-02-27 08:41:36

标签: vb.net

我有这个查询,在分配给另一个人之前检查是否已经分配了一个时间表。问题是,第一个查询执行得很好,但是如果SELECT查询没有返回任何行,则应该执行下一个查询的INSERT查询。

这是示例代码;

Private Sub DisplayConflictSchedTIMEROOM()
        Dim strConn As String = My.Settings.SLCBRegistrarDBConnectionString
        Dim sqlCon As SqlConnection = New SqlConnection(strConn)
        Try
            sqlCon.Open()
            Dim QUERY As String
            QUERY = "SELECT ListofSubjects.[Course No.], ListofSubjects.[Descriptive Title], CSchedSubTD.TimeAndDay, UtlyRoom.RoomName, CSchedSubInstructor.NameInit, CSchedSubSect.Section " &
                    "FROM CSchedMAIN INNER JOIN CSchedClass ON CSchedMAIN.SubjCode = CSchedClass.id INNER JOIN ListofSubjects ON CSchedClass.Subj = ListofSubjects.SubjectID INNER JOIN SemesterList ON CSchedMAIN.SemID = SemesterList.SemID INNER JOIN SchoolYear ON CSchedMAIN.SYID = SchoolYear.[SY ID] INNER JOIN CSchedSubTD ON CSchedMAIN.TDCode = CSchedSubTD.TDCode INNER JOIN UtlyRoom ON CSchedMAIN.RoomID = UtlyRoom.RoomID INNER JOIN CSchedSubSect ON CSchedMAIN.Section = CSchedSubSect.id INNER JOIN CSchedSubInstructor ON CSchedMAIN.InstID = CSchedSubInstructor.EmpID " &
                    "WHERE (CSchedSubTD.TimeAndDay = '" & cmbtimeday.Text & "') AND  (UtlyRoom.RoomName = '" & cmbRoom.Text & "') AND  (SchoolYear.[School Year] = '" & cmbSY.Text & "') AND (SemesterList.Description = '" & cmbSemester.Text & "')"
            CMD = New SqlCommand(QUERY, sqlCon)
            Reader = CMD.ExecuteReader
            While Reader.Read
                If Reader.HasRows Then
                    ConflictMessagebox.ShowDialog()
                Else
                   INSERTSched()
                End If
            End While
            sqlCon.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Private Sub INSERTSched()
        Dim strConn As String = My.Settings.SLCBRegistrarDBConnectionString
        Dim sqlCon As SqlConnection = New SqlConnection(strConn)
        Try
            CMD = New SqlCommand
            CMD.Connection = sqlCon
            CMD.CommandText = "InsertClassSched"
            CMD.CommandType = CommandType.StoredProcedure
            'Post your class schedule
            CMD.Parameters.AddWithValue("@Course", cmbCOURSE.Text)
            CMD.Parameters.AddWithValue("@Section", txtSection.Text)
            CMD.Parameters.AddWithValue("@Department", cmbDepartment.Text)
            CMD.Parameters.AddWithValue("@CourseNo", CMBCourseNo.Text)
            CMD.Parameters.AddWithValue("@DescTitle", cmbDescription.Text)
            CMD.Parameters.AddWithValue("@TimeDay", cmbtimeday.Text)
            CMD.Parameters.AddWithValue("@Room", cmbRoom.Text)
            CMD.Parameters.AddWithValue("@Instructor", cmbInst.Text)
            CMD.Parameters.AddWithValue("@Semester", cmbSemester.Text)
            CMD.Parameters.AddWithValue("@SY", cmbSY.Text)
            sqlCon.Open()
            CMD.ExecuteNonQuery()
        Catch sqlex As Exception
            MessageBox.Show(sqlex.Message)
        End Try
        sqlCon.Close()
        ClassSchedule()
    End Sub

请帮忙!

0 个答案:

没有答案