2014-03-25 46 views
-1

因此,這裏是我有:將SQL語句結果顯示到標籤中?

Dim connection As OleDbConnection = CompanyDB.GetConnection 
Dim invoiceCount As String = 
     "SELECT COUNT(CustomerID) " & 
     "FROM Invoices " & 
     "WHERE CustomerID = """ & customerID & """" 
    Dim selectCount As New OleDbCommand(invoiceCount, connection) 
    Try 
     connection.Open() 
     Dim reader2 As OleDbDataReader = selectCount.ExecuteReader(CommandBehavior.SingleRow) 


     If reader2.Read Then 
      frmCustomerMaintenance.lblIncidents.Text = CInt(reader2("CustomerID")).ToString 
     End If 
     reader2.Close() 
    Catch ex As OleDbException : Throw ex 
    Finally : connection.Close() 
    End Try 

我一直在第8行收到一個錯誤:

Dim reader2 As .... 

錯誤說,在條件表達式

數據類型不匹配。

我錯過了什麼嗎?顯然,這是一個數據類型不匹配,但我不知道怎樣......

回答

1

使用參數來解決您的問題(並避免SQL注入):

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

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