2017-03-22 3 views
0

私はを使用しています.Net Managed Data Access Clientは.netです。パラメータからタイムスタンプ(形式:MM/dd/yyyy hh:mi:ss:ff AM)を渡す必要があります。また、TIME_STAMP()関数を使用してoracle固有のタイムスタンプに変換する必要があります。私が値を直接注入すると、それは機能します。しかし、私はパラメータを通過すると、私はエラーが発生します。私は、それがvarchar/stringではなくオブジェクトとしてパラメータを取ると信じています。odp.netパラメータ値として渡された日付を解析する方法は?

したがって、値を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

+0

を試してみてください正しいタイプ。 –

+0

これらの{0}プレースホルダは何ですか?これらは有効なOracleパラメータ識別子ではありません。クエリは 'WHERE rownum <= {0}'ではなく 'WHERE rownum <=:num 'で終わる必要があります。文字列操作を使用してクエリを生成していますか?それはあなたにSQLインジェクション攻撃や変換エラーにさらされることを保証しています。 –

+0

@PanagiotisKanavosごめんなさい。ただ説明するだけでした。それはうまくいくだろう。しかし、oracleパラメータと同じ値を渡すと、それはしません。私はデータを取得しますが、それらは指定されたタイムスタンプに正確にはありません。タイムスタンプの形式は、02/09/2017 10:47:01:273161000 AMです。 –

答えて

1

あなたは直接パラメータ値を追加、すなわち代わりに

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}; 
+0

私はあなたの方法といくつかの方法を試しましたが、私はなぜそれが動作しているのか分かりません。上記の写真のように、私は指定された時間からデータを取得しません。しかし、sqlPlus/developerの値を使ってクエリを直接実行すると、必要な答えが得られます。 –

+1

@ Avi-Bは、あなたがアクチュアリーしたものと実際の結果を投稿します。この回答に記載されているものを試していないか、* different *クエリを実行したかのいずれかです。もう1つの可能性は、日付文字列をハードコードした月と日を切り替えたことです。パラメータは壊れておらず、フォーマット変換にも悩まされません –

関連する問題