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

标签 mysql vb.net datatable

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

 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/

相关文章:

c# - 如何使用ArrayList的 "CopyTo"方法到另一个ArrayList对象?

c# - 带有 DataTableSource 的 DataGridView ComboBox 列

mysql - 如何将这 2 个 SQL 查询合并为一个

用户的 mysql 统计数据 - 如何获取每个日期范围的列?

php - 在php在线考试系统中使用rand order时问题重复

Angular ngx-datatable cellTemplate 未加载

c# - 如何使用 LINQ 对多列数据表进行排序

mysql - 为什么我可以对内联 SELECT 值进行排序但不能在 WHERE 子句中使用它?

c# - .NET 应用程序和性能监控

c# - Windows 10 通用应用入门