美好的一天这是我之前问题的延续,因为它已经解决了。
现在的问题是我在phpMyAdmin中测试我的SQL查询时返回了一个值
这意味着rd.GetString(0)
的值为" 3"
但是当vb.net加载该值时,它将返回错误:
Additional information: Data is Null. This method or property cannot be called on Null values.
为什么Data是NULL但我的SQL截图工作正常?
这是引用
的错误If Convert.ToDouble(rd.GetString(0)) >= 80.0 Then
或者我用这个
If rd.GetDecimal(0) >= 80.0 Then
相同的结果
我的整个代码:
Dim startDayMonth As DateTime = FirstDayOfMonth(Now).AddHours(6)
Dim lastDayMonth As DateTime = LastDayOfMonth(Now).AddHours(22)
Dim qry As String = "SELECT SUM(totalWorkHour) as totalWorkHour FROM tbltimesheet WHERE studInfoID=@id " _
& "AND timeIn BETWEEN @checkAM AND @checkPM"
Using con As New MySqlConnection("server=192.168.*.***; user=dummyUsername; password=dummyPasswrd; database=dummyDBName;"), _
cmd As New MySqlCommand(qry, con)
cmd.Parameters.Add("@id", MySqlDbType.Int32).Value = Convert.ToInt32(Me.txtStudID.Text)
cmd.Parameters.Add("@checkAM", MySqlDbType.DateTime).Value = startDayMonth
cmd.Parameters.Add("@checkPM", MySqlDbType.DateTime).Value = lastDayMonth
con.Open()
Using rd As MySqlDataReader = cmd.ExecuteReader()
'Better practice here would be to return the value (if any) from this function
If rd.Read() Then
If rd.GetDecimal(0) >= 80.0 Then
MsgBox("Hurray! You already reach your target: " & rd.GetString(0), MsgBoxStyle.Information, Title:="Result")
Else
MsgBox("Your Total Work Hour: " & rd.GetString(0), MsgBoxStyle.Information, Title:="Result")
End If
Else
MsgBox("No Record Found", MsgBoxStyle.Critical, Title:="Error")
End If
End Using
End Using
rd.Close()
con.Close()
答案 0 :(得分:1)
这里有很多小修正:
'Just leave these as a datetime
Dim StartDayMonth As DateTime = FirstDayOfMonth(Now).AddHours(6)
Dim LastDayMonth As DateTime = LastDayOfMonth(Now).AddHours(22)
Dim qry As String = _
"SELECT SUM(totalWorkHour) as totalWorkHour " & _
" FROM tbltimesheet " & _
" WHERE studInfoID=@id AND timeIn BETWEEN @checkAM AND @checkPM"
'.Net uses Connection Pooling for database objects...
' This means create a new connection for every query in most cases
' but limit the scope and duration of the connection as much as possible
' (wait until the last possible moment to call .Open())
Using con As New MySqlConnection("connection string here"), _
cmd As New MySqlCommand(qry, con)
cmd.Parameters.Add("@id", MySqlDbType.Int32).Value = Convert.ToInt32(Me.txtStudID.Text)
cmd.Parameters.Add("@checkAM", MySqlDbType.DateTime).Value = StartDayMonth
cmd.Parameters.Add("@checkPM", MySqlDbType.DateTime).Value = LastDayMonth
con.Open()
Using rd As MySqlDataReader = cmd.ExecuteReader()
'Better practice here would be to return the value (if any) from this function
If rd.Read()
Dim hours As Double = rd.GetDouble(0)
If hours >= 80.0 Then
MsgBox("Hurray! You already reach your target: " & hours.ToString(), MsgBoxStyle.Information, Title:="Result")
Else
MsgBox("Your Total Work Hour: " & hours.ToString(), MsgBoxStyle.Information, Title:="Result")
End If
Else
MsgBox("No Record Found", MsgBoxStyle.Critical, Title:="Error")
End If
End Using
End Using
现在我也可以看到模块了,你想要更像这样的代码:
Module db
'Note that this is PRIVATE
' Goal is to make sure any db access in your code really
' does use the interface provided by your db module.
Private ConnectionString As String = "server=192.168.*.***; user=******; password=*****; database=dbsalog;"
'Note the use of types here in the function definition.
'Also note how I separate concerns here:
' Limit functions in this Module to just database access doing database things
' Let your presentation code in your form worry about MsgBox() and other concerns
Public Function GetWorkHours(StudentId As Integer) As Double
Dim StartDayMonth As DateTime = FirstDayOfMonth(Now).AddHours(6)
Dim LastDayMonth As DateTime = LastDayOfMonth(Now).AddHours(22)
Dim qry As String = _
"SELECT SUM(totalWorkHour) as totalWorkHour " & _
" FROM tbltimesheet " & _
" WHERE studInfoID=@id AND timeIn BETWEEN @checkAM AND @checkPM"
Using con As New MySqlConnection(ConnectionString), _
cmd As New MySqlCommand(qry, con)
cmd.Parameters.Add("@id", MySqlDbType.Int32).Value = StudentId
cmd.Parameters.Add("@checkAM", MySqlDbType.DateTime).Value = StartDayMonth
cmd.Parameters.Add("@checkPM", MySqlDbType.DateTime).Value = LastDayMonth
con.Open()
Using rd As MySqlDataReader = cmd.ExecuteReader()
If rd.Read() Return rd.GetDouble(0)
End Using
End Using
Return Nothing
End Function
End Module
您可以从程序的表单或其他区域调用,如下所示:
Dim workHours As Double = db.GetWorkHours(Convert.ToInt32(Me.txtStudId.Text))
If workHours = Nothing Then
MsgBox("No Record Found", MsgBoxStyle.Critical, Title:="Error")
ElseIf workHours >= 80.0 Then
MsgBox("Hurray! You already reach your target: " & workHours.ToString(), MsgBoxStyle.Information, Title:="Result")
Else
MsgBox("Your Total Work Hour: " & workHours.ToString(), MsgBoxStyle.Information, Title:="Result")
End If