【问题标题】:How Convert system.data.dataRow value to integer如何将 system.data.dataRow 值转换为整数
【发布时间】:2016-06-19 10:26:45
【问题描述】:

我在获取数据行值并转换为整数时遇到了问题。该函数将通过 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。

【问题讨论】:

  • 该查询不需要 DataAdapter 和 DataTable。 rows = Convert.ToInt32(cmd.ExecuteScalar) 会起作用。确保有匹配的数据

标签: mysql vb.net datatable


【解决方案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

两种方法都可以得到总值

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多