将结果从SQL语句放入标签

时间:2014-01-23 17:11:36

标签: sql vb.net ms-access

我有一个名为lblHours的标签,如何将下面的SQL语句的结果放入此标签中?

  Dim SqlQuery1 As String = "SELECT SUM(Hours) FROM TimeSheet WHERE StaffID='" &
     cbStaffID.Text & "' AND TimeSheetMonth='" & cbMonth.Text & "'"

2 个答案:

答案 0 :(得分:1)

示例

Dim con As SqlConnection = "your Connection String"
    Dim cmd As SqlCommand
    Dim query As String = "SELECT SUM(Hours) FROM TimeSheet WHERE StaffID='" &
 cbStaffID.Text & "' AND TimeSheetMonth='" & cbMonth.Text & "'"
    cmd = New SqlCommand(query, con)
    Try
    con.open
        Dim myreader As SqlDataReader = cmd.ExecuteReader()
        If myreader.Read() Then
            yourlable.text = myreader.GetValue(0)

        End If
        myreader.Close()
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
    con.Close()

答案 1 :(得分:1)

有几种不同的方法可以解决这个问题。

此Microsoft文章介绍了如何使用OLEDB连接到Access数据库...

http://support.microsoft.com/kb/821765

这是一种解决方法。还有其他方法可以连接到Access DB;你需要对基础知识做一些研究。