在标签中显示SQL结果?

时间:2014-03-28 13:32:09

标签: sql vb.net

Dim invoiceCount As String =
        "SELECT COUNT(CustomerID) " &
        "FROM Invoices " &
        "WHERE CustomerID = @customerID"

    Dim selectCount As New OleDbCommand(invoiceCount, connection)
    selectCount.Parameters.AddWithValue("@customerID", customerID)

    Try
        connection.Open()
        Dim reader2 As OleDbDataReader = selectCount.ExecuteReader(CommandBehavior.SingleRow)

        If reader2.Read Then
            frmCustomerMaintenance.lblIncidents.Text = 'what do I put here?
        End If
        reader2.Close()
    Catch ex As OleDbException : Throw ex
    Finally : connection.Close()
    End Try

我一直在搞乱这一段时间,我尝试的所有内容都会返回错误。我对SQL仍然是相当新的但是需要这样做。我只想将查询结果存储在标签中,以显示客户输入的记录数量。

2 个答案:

答案 0 :(得分:1)

这是您要搜索的内容:

frmCustomerMaintenance.lblIncidents.Text = reader2[0].ToString()

答案 1 :(得分:1)

嗯,要阅读第一列,您可以这样做:

frmCustomerMaintenance.lblIncidents.Text = reader.GetValue(0).ToString()

但是,如果您只是从一行中读取一列,那么只需拨打ExecuteScalar而不是ExecuteReader,就更容易了:

frmCustomerMaintenance.lblIncidents.Text = selectCount.ExecuteScalar().ToString()