我的Datareader不想要Dataread

时间:2014-04-30 07:37:46

标签: .net vb.net datareader

这是我的代码:

   Public Function AccidentReport(machineID As Integer) As Integer
    Dim Colours As Integer
    connection.Open()

    command = New SqlCommand("Select [Date],[Investigation Date] From MaintenanceRecord Where [Record Type] ='Accident Report' AND ID=@ID", connection)
    command.Parameters.AddWithValue("ID", machineID)
    datareader = command.ExecuteReader()

    While datareader.Read()
        If datareader.Item("Date") = "" Then
            Colours = 1
            '^Red
        ElseIf datareader.Item("Date") <> "" And datareader.Item("Investigation Date") <> "" Then
            Colours = 3
            '^Green
        End If
    End While
    datareader.Close()
    connection.Close()
    Return Colours
End Function

目前我选择的表格中没有记录,所以我想这就是问题所以我的问题是Can i force my Datareader to set Colours=1 if it doesn't read any data or is there a way I could force this before the Datareader in VB.NET?

1 个答案:

答案 0 :(得分:0)

在执行read

之前设置值
...
Colours = 1 ' Add this line
While datareader.Read()
    If datareader.Item("Date") = "" Then
        Colours = 1
        '^Red
    ElseIf datareader.Item("Date") <> "" And datareader.Item("Investigation Date") <> "" Then
        Colours = 3
        '^Green
    End If
End While
...

或者@Satpal声明,只需使用默认值

初始化变量
Dim Colours As Integer = 1