我将一个sql语句写入vb.net以从msaccess获取数据,其中date等于patepicker控件。但这里语法错误。 我的代码如下: -
Dim ct As String = "select * from add_student where _Date <= #" & dtDate1.ToString("MM/dd/yyyy") & "#"
cmd = New OleDbCommand(ct)
cmd.Connection = con
rdr = cmd.ExecuteReader()
注意: - _Date列在ms访问数据库中具有日期/时间数据类型 请建议我,哪里是错误
答案 0 :(得分:1)
您应该始终使用参数:
dim sqlQuery ="Select * from add_student where _Date = ?"
using conn = New OleDbConnection("connectionstring")
conn.open()
Using selectCmd As New OleDbCommand(sqlQuery, conn)
selectCmd.Parameters.Add("_Date", OleDbType.Date).Value = dtDate1
using rdr = selectCmd.ExecuteReader()
while rdr.read()
End While
End Using
End Using
End Using
答案 1 :(得分:0)
您的问题可能是&#34; /&#34;不是斜杠,而是本地化的日期分隔符。
因此,强制斜线:
Dim ct As String = "select * from add_student where _Date <= #" & dtDate1.ToString("MM'/'dd'/'yyyy") & "#"
并且可能是奇怪字段名称的括号:
Dim ct As String = "select * from add_student where [_Date] <= #" & dtDate1.ToString("MM'/'dd'/'yyyy") & "#"