如何确保OleDbCommand参数的日期格式正确? [英] How to ensure dates are in correct format for OleDbCommand parameters?

查看:92
本文介绍了如何确保OleDbCommand参数的日期格式正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我针对MDB数据库执行以下代码时,数据表为空,但是当我在针对数据库的查询工具中运行此代码时,它将返回2条记录.

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.

可能是什么问题?

参数的日期格式是否存在问题(例如8/5/13与5/8/13)?

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

推荐答案

我认为您是对的.我建议指定一个参数类型

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

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

这篇关于如何确保OleDbCommand参数的日期格式正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆