继续关于在两个日期之间获取日期

时间:2016-09-03 13:54:48

标签: c# sql

我的datagridview显示2015年的日期,即使我从两个不同的日期设置我的两个datetimepickers,但同一年(2016年)。

继承我的代码..

public static List<Retailer> GetDataAllRetailer(DateTime past, DateTime present)
        {
        List<Retailer> data = new List<Retailer>();

        MySqlConnection con = DBConnection.ConnectDatabase();
        try
        {   // AND
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + tablename + " WHERE (date BETWEEN '" + past.ToString("MM-dd-yyyy") + "' AND '" + present.ToString("MM-dd-yyyy") + "') ", con);

            MySqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Retailer rawData = new Retailer();
                    rawData.Date = reader.GetString(1);
                    rawData.Walletid = reader.GetString(0);
                    rawData.Fname = reader.GetString(2);
                    rawData.Lname = reader.GetString(3);
                    rawData.Birthdate = reader.GetString(4);
                    rawData.Address = reader.GetString(5);
                    rawData.Province = reader.GetString(6);
                    rawData.City = reader.GetString(7);
                    rawData.Balance = reader.GetDouble(8);
                    rawData.Frozen = reader.GetDouble(9);
                    rawData.Sponsor_id = reader.GetString(10);
                    rawData.Share = reader.GetDecimal(11);
                    rawData.Email = reader.GetString(12);
                    rawData.Password = reader.GetString(13);
                    rawData.Type = reader.GetInt32(14);
                    data.Add(rawData);


                }
            }



            reader.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }

        return data;
    }

}

datagrid

1 个答案:

答案 0 :(得分:1)

如果表列是varchar,那么我建议你将它转换为日期以适当地进行搜索,如下所示:

 SELECT * FROM Table WHERE 
 CONVERT(DATE, FromDate) >= CONVERT(DATE, '2016-09-03')
 AND CONVERT(DATE, ToDate) <= CONVERT(DATE, '2016-09-13')