如何在两个日期Asp.net之间获取记录

时间:2014-11-29 12:25:32

标签: asp.net

我想在两个日期之间搜索记录,但此代码无效。

protected void BtnSearch_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    string ToDate = txtDateTo.Text;
    string DateFrom = txtDateFrom.Text;
    dt = NewLeadClass.GetReport(ToDate, DateFrom);
}    


public static DataTable GetReport(string ToDate,string FromDate)
{
 SqlParameter[] param = new SqlParameter[]
 {
           new SqlParameter("@ToDate", ToDate),
           new SqlParameter("@FromDate",FromDate)   
       };
 DataSet ds = SqlHelper.ExecuteDataset(Utility.GetConString(), CommandType.Text, "Select * from EmpDailyReport where RecordDate <= @ToDate and RecordDate>= @FromDate",param);
    return ds.Tables[0];
}

2 个答案:

答案 0 :(得分:0)

更改您的查询:

 DataSet ds = SqlHelper.ExecuteDataset(Utility.GetConString(), CommandType.Text, "Select * from EmpDailyReport where RecordDate BETWEEN  @FromDate  and @ToDate 

   changesdone here :   "where RecordDate BETWEEN  @FromDate  and @ToDate "         

答案 1 :(得分:0)

这是因为您要比较两个字符串而不是&#34; date&#34;,因此请尝试将日期解析为日期或日期时间对象并重试。

Ps:在你的where子句中你可以使用类似的东西:

where RecordDate between FromDate and ToDate