Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

c# - Add date parameter to oracle query

What I'm trying to do is to execute the query between two dates, and the dates would come from a parameter.

In the below code, I'm putting one parameter and letting the second date as fixed just for example purposes.

The code / query is kind of long to paste it here, but here is a short version:

OracleParameter fromDateParameter = new OracleParameter();
fromDateParameter.OracleDbType = OracleDbType.Varchar2;
fromDateParameter.Value = "'3/06/2013 20:00:00'";

this.oracleDataAdapter4.SelectCommand = new OracleCommand("
   SELECT DISTINCT (LOG.RID) FROM LOG WHERE LOG.TIMESTAMP 
   BETWEEN TO_DATE(:fromDateParameter, 'MM/DD/YYYY hh24:mi:ss ') 
   AND TO_DATE('3/06/2013 23:59:00', 'MM/DD/YYYY hh24:mi:ss '))", 
   oracleConnection4);

oracleDataAdapter4.SelectCommand.Parameters.Add(fromDateParameter);           
this.oracleDataAdapter4.Fill(event11);

And I got this message:

ORA-01858: a non-numeric character was found where a numeric was expected

I did the same query without the parameter and it works just fine.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I didn't find a way to do it using Varchar2 Datatype. So, I'm giving the answer using Date Datatype.

if (dateTo.Minute > 30)
   minToInt = 30;
else
   minToInt = 00;

dateFrom = DateTime.Now;
dateTo = DateTime.Now;     

DateTime dateFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dateFrom.Hour, 00, 00);
DateTime dateTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, dateTo.Hour, minToInt, 00);

////////FROM DATE/////////
OracleParameter fromDateParameter = new OracleParameter();
fromDateParameter.OracleDbType = OracleDbType.Date;
fromDateParameter.Value = dateFrom;

////////TO DATE/////////
OracleParameter toDateParameter = new OracleParameter();
toDateParameter.OracleDbType = OracleDbType.Date;
toDateParameter.Value = dateTo;


this.oracleDataAdapter4.SelectCommand = new OracleCommand("
   SELECT DISTINCT (LOG.RID) FROM LOG WHERE LOG.TIMESTAMP 
   BETWEEN :fromDateParameter 
   AND :toDateParameter)", oracleConnection4);

oracleDataAdapter4.SelectCommand.Parameters.Add(fromDateParameter);           
oracleDataAdapter4.SelectCommand.Parameters.Add(toDateParameter);  
this.oracleDataAdapter4.Fill(event11);

Don't get confuse with the if/else minute validation, it is just for my query purpose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...