2014-03-28 63 views
-2
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仍然比较陌生,但这需要完成。我只是想查询的结果存储在标签中显示多少条记录中输入客户有..在标签中显示SQL结果?

回答

1

好,读的第一列,你可以这样做:

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

但是,如果你只从单排读一列,这样,它更容易只是调用ExecuteScalar而不是ExecuteReader

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

这工作最简单!谢谢。 [我会将它标记为答案,当它让我] –

1

这是你正在寻找什么:

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