OleDbException:ORDER BY 子句中的语法错误
Posted
技术标签:
【中文标题】OleDbException:ORDER BY 子句中的语法错误【英文标题】:OleDbException: Syntax error in ORDER BY clause 【发布时间】:2012-09-12 17:20:58 【问题描述】:我在测试如何通过选择所需列然后指定唯一 ID(即按主 ID、ID 排序的房间号)来获取最新数据库条目时遇到此错误。
这是我的代码:
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\patientinfo.accdb"
Conn.Open()
Dim statement As String = " SELECT room_number, patient_name,patient_age,date_confinement,type_sickness, type_fluid, bottle_used, drop_rate FROM tblPatientInfo WHERE room_number=1 ORDER BY ID DESC LIMIT 1"
RetrieveInfos(statement)
End Sub
Public Sub RetrieveInfos(ByRef statement As String)
Dim cmd As OleDbCommand = New OleDbCommand
With cmd
.CommandText = statement
.CommandType = CommandType.Text
.Connection = Conn
.ExecuteNonQuery()
'--read records in access database----------------
Dim reader As OleDbDataReader = cmd.ExecuteReader
While reader.Read
lblName.Text = (reader.GetString(0))
lblAge.Text = (reader.GetInt32(1).ToString)
lblDate.Text = (reader.GetDateTime(2).ToString)
lblSickness.Text = (reader.GetString(3))
lblFluid.Text = (reader.GetString(4))
lblBottle.Text = (reader.GetInt32(5).ToString)
lbldrops.Text = (reader.GetInt32(6).ToString)
End While
reader.Close()
End With
End Sub
任何帮助将不胜感激。谢谢!
【问题讨论】:
连接字符串是什么(提供者)? 【参考方案1】:“LIMIT 1”子句是可疑的。建议修改查询以使用“SELECT TOP 1 .....”
Dim statement As String = " SELECT TOP 1 room_number, patient_name,patient_age,
date_confinement,type_sickness, type_fluid, bottle_used, drop_rate FROM
tblPatientInfo WHERE room_number=1 ORDER BY ID DESC"
(为清楚起见,此处使用硬返回包裹查询)。
【讨论】:
【参考方案2】:限制特定于 mysql。
对于 Access 或 SQL Server:
Select top 1 from ...
Order by ...
对于甲骨文:
Select *
from
(
Select ...
Order by ...
)
Where row_num = 1
【讨论】:
以上是关于OleDbException:ORDER BY 子句中的语法错误的主要内容,如果未能解决你的问题,请参考以下文章