How to ensure dates are in correct format for OleDbCommand parameters?

对着背影说爱祢 提交于 2020-01-07 05:41:12

问题


When I execute the code below against an MDB database, the data table is empty, however when I run this in a query tool against the database it returns 2 records.

What could be the problem?

Is it an issue with the date format of the parameters (ie. 8/5/13 vs 5/8/13)?

Using oDB As OleDbConnection = GetDbConnection()
    Using oCmd As New OleDbCommand("SELECT * " & _
            " FROM Table1, Table2" & _
            " WHERE (Table1.Date BETWEEN @Date1 AND @Date2) AND (Table1.Id IS NULL) AND (Table2.number = Table1.num) AND (Table1.code1 = Table2.code1) ", oDB)
        oCmd.Parameters.AddWithValue("@Date1", Date.Today)
        oCmd.Parameters.AddWithValue("@Date2", Date.Today.AddDays(Me.intDaysAhead))
        oDB.Open()
        dt = New DataTable()
        Using da As OleDbDataAdapter = New OleDbDataAdapter(oCmd)
            da.Fill(dt)
        End Using
    End Using
End Using

回答1:


I think you are right. I recommend to specify a parameter type OleDbType

 oCmd.Parameters.Add("@Date1", OleDb.OleDbType.Date).Value = Date.Today


来源:https://stackoverflow.com/questions/16445119/how-to-ensure-dates-are-in-correct-format-for-oledbcommand-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!