2016-03-05 69 views
0

我在獲取數據行值並將其轉換爲整數時遇到問題。該函數將通過數據表方法從數據庫中檢索數據和計數。我成功獲得計數值,並且我想使用該值來計算員工缺勤日。如何將system.data.dataRow值轉換爲整數

Dim table As New DataTable 
Dim AttendCommand As New MySqlCommand("SELECT COUNT(EmployeeID) AS AttendDay FROM employee.attendance WHERE [email protected] AND [email protected]", 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 

我從debug得到的結果是countTotal = 0。

+0

你不需要一個DataAdapter和DataTable該查詢。 'rows = Convert.ToInt32(cmd.ExecuteScalar)'會工作。確保有匹配的數據 – Plutonix

回答

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 

兩個methold可以得到總價值