使用条件从数据集中获取记录

时间:2012-01-11 10:59:51

标签: c# dataset

我想从数据集中获取fromdate到todate之间的一些记录。但是同一列中的fromdate和todate都是..

 **Expression :** 
  string expression = "punchDate >='" + txtfromdate.Text + "' and PunchDate <='" + txttodate.Text + "' ";
            findbyexpression(expression);

此表达式在同一个月返回记录。假设我想从'12 -02-2011'到10-01-2011获取记录'意味着它不会返回记录......

 protected void findbyexpression(string Expression)
{
    try
    {
        DataTable dt1 = new DataTable();
        DataSet4TableAdapters.sp_getalltimesheetTableAdapter TA = new DataSet4TableAdapters.sp_getalltimesheetTableAdapter();
        DataSet4.sp_getalltimesheetDataTable DS = TA.GetData();
        DataTable DT = new DataTable();
        DT = DS[0].Table;
        DataRow[] foundRows;
        foundRows = DT.Select(Expression);
        dt1 = foundRows.CopyToDataTable();
        Session["TaskTable"] = dt1;
        grdvw.DataSource = Session["TaskTable"];
        grdvw.DataBind();
    }
    catch (Exception e2)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('" + e2.Message + "');</script>", false);
    }
}

1 个答案:

答案 0 :(得分:0)

    string expression = string.Format("punchDate>=convert(datetime,'{0}',105) and PunchDate <=convert(datetime,'{1}',105) ", txtfromdate.Text,txttodate.Text);

//这可能有效!