如何解析作为odp.net参数值传递的日期?

时间:2017-03-22 09:16:29

标签: c# oracle odp.net

我正在使用.net的 Oracle托管数据访问客户端。我需要从参数传递我的时间戳(格式: MM / dd / yyyy hh:mi:ss:ff AM ),并且还必须使用TIME_STAMP()函数将其转换为oracle特定的时间戳。如果我直接注入值,它就可以工作。但是,如果我通过参数,我会收到错误。我相信,它将参数作为对象而不是varchar / string。

那么,我如何将我的值作为OracleParameter传递并将其转换为特定于oracle的timeStamp。

但这不会。这将显示日期的数据,但不显示指定的正确时间戳(我甚至需要比较秒数)。

string SELECTGROUPSESSIONS = @"SELECT * FROM (
                                                SELECT Recent.sent_date, Recent.thread_id, Recent.body_string, Recent.body_text, 
                                                Recent.message_string, Recent.message_text, Recent.body_len, Recent.from_jid , 
                                                Recent.to_jid, Recent.history_flag 
                                                FROM JM Recent Left Join 
                                                (Select * from JM where sent_date > TO_TIMESTAMP(:FromHistory,'MM/dd/yyyy hh:mi:ss:ff AM')) Old 
                                                on (Recent.body_string=Old.body_string and 
                                                Recent.body_len=Old.body_len and Recent.from_jid=Old.from_jid and 
                                                REGEXP_REPLACE( Recent.to_jid , '([/])\w+', '') = REGEXP_REPLACE( Old.to_jid , '([/])\w+', '') 
                                                and Recent.history_flag=Old.history_flag and Old.sent_date < Recent.sent_date) 
                                                where Recent.msg_type ='g' 
                                                and Recent.body_len>0 and Recent.sent_date > TO_TIMESTAMP(:FromDate,'MM/dd/yyyy hh:mi:ss:ff AM')  
                                                and Recent.sent_date <  TO_TIMESTAMP(:ToDate,'MM/dd/yyyy hh:mi:ss:ff AM')
                                                and Old.sent_date is null 
                                                order by Recent.sent_date asc  
                                                ) 
                                                WHERE rownum  <= {0}";

对于值:

enter image description here

我的查询应该是这样的。

SELECT * FROM ( 
                        SELECT Recent.sent_date, Recent.thread_id, Recent.body_string, 
                        Recent.body_text, Recent.message_string, Recent.message_text, Recent.body_len, 
                        Recent.from_jid , Recent.to_jid, Recent.history_flag FROM JM Recent Left Join 
                        (Select * from JM where sent_date > 
                        TO_TIMESTAMP('02/19/2017 10:43:00:8357400 AM','MM/dd/yyyy hh:mi:ss:ff AM') ) Old on 
                        (Recent.body_string=Old.body_string and Recent.body_len=Old.body_len and 
                        Recent.from_jid=Old.from_jid and 
                        REGEXP_REPLACE( Recent.to_jid , '([/])\w+', '') = REGEXP_REPLACE( Old.to_jid , '([/])\w+', '') 
                        and Recent.history_flag=Old.history_flag and Old.sent_date < Recent.sent_date) 
                        where Recent.msg_type ='g' and Recent.body_len>0 and Recent.sent_date > 
                        TO_TIMESTAMP('03/21/2017 10:43:00:8357400 AM','MM/dd/yyyy hh:mi:ss:ff AM') and 
                        Recent.sent_date < TO_TIMESTAMP('03/22/2017 09:02:28:3049506 AM','MM/dd/yyyy hh:mi:ss:ff AM') 
                        and Old.sent_date is null order by Recent.sent_date asc  ) WHERE rownum <= 500 

C#代码:

selectCommand = _factory.GetDbCommand(queryStatement, SqlConnection);

            selectCommand.Parameters.Clear();
            using (selectCommand)
            {
                if (parameters != null)
                {
                    foreach (var param in parameters)
                    {
                        selectCommand.Parameters.Add(_factory.CreateParameter(param.Key, param.Value));                           

                    }
                }

                try
                {                    

                    using (
                        var reader = selectCommand.ExecuteReader())
                    {
                        processReader(reader);
                    }

结果: 可能是因为日期格式不匹配?

enter image description here

1 个答案:

答案 0 :(得分:2)

您应该尝试直接添加参数值,即代替

string SELECTGROUPSESSIONS = "SELECT ...
... sent_date > TO_TIMESTAMP(:FromHistory,'MM/dd/yyyy hh:mi:ss:ff AM')

string SELECTGROUPSESSIONS = "SELECT ...
... sent_date > :FromHistory ..."

selectCommand.Parameters.Add("FromHistory", OracleDbType.TimeStamp, ParameterDirection.Input).Value = {the C# DateTime value};
相关问题