使用'限制'的MySQL语句错误

时间:2011-05-19 09:07:12

标签: mysql vb.net odbc

我正在尝试按CustomerID选择最新的OrderID。但它一直说我的SQL语法有错误,即使它在PhpMyAdmin中有效。我正在使用MySQL数据库和ODBC connector

Public Function GetOrderIDByCustID(ByVal CustID As Integer) As Integer
myconn.Open()

Dim OrderID As Integer = 0
Dim OrderSQL As String = 
"SELECT OrderID 
FROM Order 
WHERE OrderCustID= ? AND OrderArchived = 0 
ORDER BY OrderDate DESC LIMIT 1"

Dim cmd As New OdbcCommand("OrderSQL", myconn)
cmd.Parameters.AddWithValue("OrderCustID", CustID)
OrderID= cmd.ExecuteScalar()

myconn.Close()

Return OrderID
End Function

所以这应该为我提供该客户最新订单的OrderID。

任何人都可以看到我可能做错了吗?谢谢!

1 个答案:

答案 0 :(得分:1)

嗯,我现在已经看了一个小时了..

Dim cmd as New OdbcCommand("OrderSQL", myconn)

OrderSQL不应该在括号中,解决方案是:

Dim cmd as New OdbcCommand(OrderSQL, myconn)

多么蹩脚大声笑..