在 VB.NET 中为 Oracle SQL 指定绑定日期参数

Specifying bound date parameters for Oracle SQL in VB.NET

希望有人能提供帮助。我在我的 VB.NET 项目中使用 Oracle.ManagedDataAccess.Client 从 Oracle 11g 数据库中提取信息。

我的项目使用硬编码字符串,但我希望将日期时间更改为绑定变量,因为我听说它在服务器负载上更容易。

我可以让下面的代码与 Using Ocommand As New OracleCommand( 和硬编码的日期时间字符串一起使用,但无法让下面的代码用直接连接和绑定变量替换 Using

    ''Build SQL Query and set variables
    Dim d_start_date As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour - 1, 0, 0)
    Dim p_start_date As OracleParameter = New OracleParameter
    p_start_date.OracleDbType = OracleDbType.Date
    p_start_date.Value = d_start_date
    p_start_date.ParameterName = "fromDateParam"

    Dim d_end_date As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0)
    Dim p_end_date As OracleParameter = New OracleParameter
    p_end_date.OracleDbType = OracleDbType.Date
    p_end_date.Value = d_end_date
    p_end_date.ParameterName = "toDateParam"


    ''Setup Datatable, connections and pull data
    Dim myDT As New DataTable
    Dim Oconn As New OracleConnection(my_CREDS & my_COMMS)

    Dim Ods As New DataSet
    Dim Ocommand As OracleCommand = New OracleCommand("select " & _
        "username as Recipient, " & _
        "to_char(CreatedDate,'YYYY-mm-dd HH24:MI:SS') as CREATEDDATE " & _
        "from " & _
        "tbl.one " & _
        "where " & _
        "CreatedDate >= :fromDateParam " & _
        " and CreatedDate < :toDateParam;", Oconn)
    Ocommand.Parameters.Add(p_start_date)
    Ocommand.Parameters.Add(p_end_date)
    Using Oda As New OracleDataAdapter(Ocommand)
        Oda.Fill(myDT)
    End Using
    Ocommand.Dispose()

这样做会在尝试填充数据表时出错:Oda.Fill(myDT)。谁能解释为什么?我对 Oracle 数据库查询还是很陌生。

经过一番折腾,我发现我在 SQL 语句的末尾添加了一个额外的 ;,取自 Oracle SQL 中的原始语句开发商。删除它后,代码执行起来就像做梦一样!