检查数据集中的null(或无结果)

时间:2015-08-12 02:16:02

标签: sql vb.net

我正在使用VisualBasic.NET中的以下代码。它本质上应该是从表中提取行ID,并考虑到特定的条件。但是,如果需要,我想建立一个绕过这些条件的故障保险。

我正在尝试编写If语句来比较Item(0),但这段代码似乎无论如何都会触发。如何比较以查看写入QuestionConnectionQuery的查询是否实际返回行?

    For i As Integer = 1 To intNoOfQuestions
        'TODO: If there are no valid questions, pull up any of them that meets the difficulty requirement....


        ' Go into the Database and retrieve a question that hasn't been selected in the last seven days.
        Dim QuestionConnectionQuery As New OleDb.OleDbCommand("SELECT TOP 1 Questions.QuestionID, Questions.QuestionCategory & ' :   ' & Questions.QuestionSubCategory AS Category FROM Questions WHERE (((Questions.QuestionDifficulty)=[?])) OR (((Questions.LastDateRevealed) Is Null)) OR ((Questions.LastDateRevealed)>=DateAdd('d',-7,Now())) ORDER BY Rnd(QuestionID);", QuestionConnection)
        QuestionConnectionQuery.Parameters.AddWithValue("?", intQuestionDifficulty(i - 1).ToString)
        Dim QuestionDataAdapter As New OleDb.OleDbDataAdapter(QuestionConnectionQuery)
        Dim QuestionDataSet As New DataSet

        QuestionDataAdapter.Fill(QuestionDataSet, "Questions")

        ' If the result is not null... add it to the array.
        If IsNothing(QuestionDataSet.Tables("Questions").Rows(0).Item(0)) Then
            ' Add that to the intQuestArray() array
            intQuestArray(i - 1) = QuestionDataSet.Tables("Questions").Rows(0).Item(0)
        Else
            MessageBox.Show("ERROR: Not enough questions.")

            QuestionDataAdapter.Fill(QuestionDataSet, "Questions")

        End If


        ' Update the database by adding today's date to the "LastDateRevealed" column of the table.
        Dim QuestionUpdateQuery As New OleDb.OleDbCommand("UPDATE Questions SET LastDateRevealed=NOW() WHERE QuestionID = [?]", QuestionConnection)
        QuestionUpdateQuery.Parameters.AddWithValue("?", QuestionDataSet.Tables("Questions").Rows(0).Item(0).ToString)
        QuestionUpdateQuery.ExecuteNonQuery()

    Next

1 个答案:

答案 0 :(得分:1)

If IsDBNull(QuestionDataSet.Tables("Questions").Rows(0).Item(0)) Then
  

https://msdn.microsoft.com/en-us/library/tckcces5(v=vs.90).aspx