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 に答える