0

我试图按日期显示数据,但我没有得到确切的答案,请帮助我..

public void gettoday()
    {
        con.Open();
        {
            string strview = @"select MRNO,MaterialCode,Description,Specification,
                              Category as Cat,Source as Sor,Population as Pop, StockInStores as Stock, MRRating as Rating,PreparedBy,PreparedDate,CheckedBy,CheckedDate,ApprovedBy,ApprovedDate  
                         from tbl_KKSMaterialRaise 
                         where PreparedDate between (getdate()-1) and (getdate()+1)";
            SqlCommand cd = new SqlCommand(strview, con);
            SqlDataReader reader = cd.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();
        }
        con.Close();


    }
4

1 回答 1

1

您的SQL查询似乎不正确。所以你想选择昨天和明天之间的记录。

那么这应该工作(假设SQL-Server因为你使用GetDate):

WHERE PreparedDate BETWEEN DateAdd(d,-1,GetDate()) AND DateAdd(d,+1,GetDate())

编辑:除此之外,您应该始终使用using-statement(对于任何实现的对象IDisposable)以确保即使出现异常也关闭连接:

const string strview = @"select MRNO,MaterialCode,Description,Specification, Category as Cat,Source as Sor,Population as Pop, StockInStores as Stock, MRRating as Rating,PreparedBy,PreparedDate,CheckedBy,CheckedDate,ApprovedBy,ApprovedDate  
                         FROM tbl_KKSMaterialRaise 
                         WHERE PreparedDate BETWEEN DateAdd(d,-1,GetDate()) AND DateAdd(d,+1,GetDate())";
// don't use global connection objects
using(var con = new SqlConnection(connectionString))
using(var cmd = new SqlCommand(strview, con))
{
    con.Open();
    GridView1.DataSource = cmd.ExecuteReader();
    GridView1.DataBind();
}
于 2012-07-18T11:15:58.173 回答