Hello,
I create a Web form project,it have A and B table, A table have ID,start_date and an end_date columns, B table have ID and name columns,
and need input a start_date and an end_date text box.
If both two text box input date to search, I want it return a date range that
table data's start_date >= input start_date and
table data's end_date >= input end_date.
I use StringBuilder to create SQL string to search, and use Parametes to phrase variable
but it doesn't work.
here's the code:
--------
StringBuilder sbStringSql = new StringBuilder(); sbStringSql.Append("select A.ID,B.NAME,A.STARTDATE,A.ENDDATE FROM A,B"); sbStringSql.Append("WHERE 1=1 AND A.ID=B.ID");
//------------------this way doesn't work------------------------------- sbStringSql.Append(" and ((to_char(A.STARTDATE,'yyyy-mm-dd')) >=:STARTDATE"); sbStringSql.Append(" and (to_char(A.ENDDATE,'yyyy-mm-dd')) <=:ENDDATE)"); if (strSTARTDATE != string.Empty) { da.SelectCommand.Parameters.Add(new OracleParameter("STARTDATE",strSTARTDATE)); } if (strENDDATE != string.Empty) { da.SelectCommand.Parameters.Add(new OracleParameter("ENDDATE", strENDDATE)); } //-----------------------------------------------
//--------this way can work---------------- sbStringSql.Append(" and ((to_char(A.STARTDATE,'yyyy-mm-dd')) >='" + strSTARTDATE + "'"); sbStringSql.Append(" and (to_char(A.ENDDATE,'yyyy-mm-dd')) <='" + strENDDATE + "')"); //--------------------------------------------
The other way, I use BETWEEN ,but:
----
sbStringSql.Append(" and ((to_char(A.STARTDATE,'yyyy-mm-dd')) between '" + strSTARTDATE + "' and '" + strENDDATE + "')");
----
or
----
sbStringSql.Append(" and ((to_char(A.STARTDATE,'yyyy-mm-dd')) between :STARTDATE and :ENDDATE)"); if (strSTARTDATE != string.Empty) { da.SelectCommand.Parameters.Add(new OracleParameter("STARTDATE",strSTARTDATE)); if (strENDDATE != string.Empty) { da.SelectCommand.Parameters.Add(new OracleParameter("ENDDATE", strENDDATE)); }
----
both these two way can't work.
But when I use the SQL string in both way with Oracle SQL developer to run query, both works.
What is the problem?