数据库中有两个字段:kssj(开始时间)和jssj(结束时间),页面上有个textbox,绑定了日历控件,还有一个按钮,现在希望使用日历控件选择日期,点击按钮后,系统根据textbox的值查找符合条件的记录(即时间在开始时间和结束时间之间的记录)。具体应该怎么操作。我需要具体代码,尤其是类型转换部分。谢谢private void Button2_Click(object sender, System.EventArgs e)
{
string strRQ;
if (this.txtStartTime.Text!="")
{
strRQ=txtStartTime.Text;
DateTime rq=Convert.ToDateTime(strRQ);   
}
else
{
Response.Write("<script>alert('请先选择日期');</script>");
return;
}
SqlConnection Conn=new SqlConnection("server=localhost;uid=sa;pwd=123456;database=test");
SqlDataAdapter da=new SqlDataAdapter("select *  from table1 where DateDiff(d,kssj,rq)>0  and DateDiff(d,jssj,rq)<0",Conn);
Conn.Open();
    DataSet ds=new DataSet();
da.Fill(ds,"table1");
//……
//……
//…… Conn.Close();
Conn.Dispose(); }

解决方案 »

  1.   

    咋不用between and ?select *  from table1 where rq between kssj and jssj
      

  2.   

    现在的问题不是用不用between的问题,我之前也是用它,现在的问题是变量rq,总是提示列名rq无效。
      

  3.   

    加单引号试试,即
    SqlDataAdapter da=new SqlDataAdapter("select *  from table1 where rq between '" +kssj +"'  and '"+ jssj +"'",Conn); 
      

  4.   


    加单引号试试,即
    SqlDataAdapter da=new SqlDataAdapter("select *  from table1 where rq between '" +kssj +"'  and '"+ jssj +"'",Conn);[/Quote]kssj和jssj是表中的字段,应该不需要加吧,rq是变量,所以问题应该出在它身上,不知道是不是上面的赋值有问题
      

  5.   

    SqlDataAdapter da=new SqlDataAdapter("select *  from table1 where DateDiff(d,kssj,rq)>0  and DateDiff(d,jssj,rq) <0",Conn);rq是变量,不能直接用的,改为下面试下SqlDataAdapter da=new SqlDataAdapter("select *  from table1 where DateDiff(d,kssj,'" + rq + "')>0  and DateDiff(d,jssj,'" + rq + "') <0",Conn);
      

  6.   

    又是    拼接语句的问题
    检查你的sql 语句
    呵呵