如何将system.data.dataRow值转换为整数

时间:2016-03-05 17:15:46

标签: mysql vb.net datatable

获取datarow值并转换为整数时遇到问题。该函数将通过数据表方法从数据库中检索数据和计数。我成功获得了计数值,我想用这个值来计算员工缺勤日。

 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.

1 个答案:

答案 0 :(得分:1)

@Plutonix非常感谢你的建议,我设法获得了价值。

 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

两种方法都可以得到总值