为什么我的基本日期选择器无法将输入转换为日期时间格式?

时间:2013-07-01 00:59:20

标签: vb.net oracle

我正在使用vb.net和oracle的基本日期选择器作为我的数据库。当我从基本日期选择器插入日期时,我收到此错误>> ORA-01797:此运算符必须后跟ANY或ALL

这是我的代码:

    Private Function GetDate(ByVal bdp1 As Date) As DataSet

    Dim connectionString As String = "Data Source = ***; User ID =***; Password =**;"
    Dim sqlConnection As OracleClient.OracleConnection = New OracleClient.OracleConnection(connectionString)

    Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= ('" & bdp1 & "' , 'MM/DD/YYYY') and tran_dttm > ('" & bdp1 & "', 'MM/DD/YYYY')"

    Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
    sqlCommand.CommandTimeout = 0

    Dim dataAdapter As OracleClient.OracleDataAdapter = New OracleClient.OracleDataAdapter(sqlCommand)
    Dim dataSet As DataSet = New DataSet
    dataAdapter.Fill(dataSet)

    Return dataSet

End Function

3 个答案:

答案 0 :(得分:0)

试试这个..

Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= cdate('" & bdp1.Value.ToString & "') and tran_dttm > cdate('" & bdp1.Value.ToString  & "')"

答案 1 :(得分:0)

试试这个:

Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= '" & bdp1.ToString("MM/dd/yyyy") & "' and tran_dttm > '" & bdp1.ToString("MM/dd/yyyy") & "'"

但对于oracle,默认的日期时间格式为YYYY-MM-DD。您有三种选择:

  • 更改适合您需求的默认文件格式
  • 使用默认格式:bdp1.ToString("yyyy-MM-dd")
  • 使用指定读取格式的todate oracle函数:
    " to_date('" & bdp1.ToString("MM/dd/yyyy") & "', 'mm/dd/yyyy') "

答案 2 :(得分:0)

请尝试:

Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= :dtm1 and tran_dttm > :dtm2"

Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.Parameters.AddWithValue("dtm1",bdp1)
sqlCommand.Parameters.AddWithValue("dtm2",bdp1)

其中a)避免了SQL注入的可能性,并且b)将日期保持为整个日期,而不是将其从字符串中删除。

它不会解决您的查询的逻辑问题 - 您正在尝试查找tran_dttm“小于或等于”“大于...的行“相同值。