将SQL语句结果显示到标签中?

时间:2014-03-25 16:44:07

标签: sql database vb.net

所以这就是我所拥有的:

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 个答案:

答案 0 :(得分: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)