在获取数据行值并将其转换为整数时遇到问题。该函数将通过数据表方法从数据库中检索数据并进行计数。我成功获得了计数值,我想使用该值来计算员工缺勤日。
Dim table As New DataTable
Dim AttendCommand As New MySqlCommand("SELECT COUNT(EmployeeID) AS AttendDay FROM employee.attendance WHERE EmployeeID=@EmployeeID AND month=@month", MysqlConn)
MysqlConn.Open()
AttendCommand.Parameters.AddWithValue("@EmployeeID", txtID.Text)
AttendCommand.Parameters.AddWithValue("@month", Cmonth)
Dim adapter = New MySqlDataAdapter(AttendCommand)
adapter.Fill(table)
If table.Rows.Count > 0 Then
test = CInt(table.Rows(0).ToString)
End If
MysqlConn.Close()
'count absence day after retrieve
Dim countTotal As Integer = 0
Dim total As Integer = 22
countTotal = total - test
我从调试中得到的结果是 countTotal=0。
最佳答案
@Plutonix 非常感谢您的建议,我设法获得了值(value)。
Dim test As Int32 = 0
Try
MysqlConn.Open()
test = Convert.ToInt32(AttendCommand.ExecuteScalar)
counTotal = total - test
Catch ex As Exception
End Try
我还确定了为什么我无法通过 dataTable 方法获取总值的问题。 (缺少.Item(0))
If table.Rows.Count > 0 Then
test = Convert.ToInt32(table.Rows(0).Item(0).ToString)
End If
两种方法都可以得到总值(value)
关于mysql - 如何将 system.data.dataRow 值转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817176/