如何将日历中选择的日期与数据库中的日期进行比较?

时间:2014-12-03 12:47:04

标签: c# database entity-framework visual-studio-2012 calendar

我为学校项目做这件事。我对C#很陌生(抱歉我的英语不好)

我正在制作预订系统,应该可以查看所选日期的可用时间。我正在使用实体框架。我创建了一个数字从0到23的数组来表示小时数。但是我希望从数组中删除保留的那些,因此只有可用的那些将被添加到下拉列表中。

首先我有一个DBHandler类:

class DBHandler
{
     public void GetReservedTimes(string hour, DateTime StartDate)
    {
        using (systemEntities forbindelse = new systemEntities())
        {
            SqlCommand command = new SqlCommand("SELECT hourofday FROM reservation WHERE date = " + StartDate + "");
        }
    }
}

我不确定这条SQL语句是否正确..

这是我的预备班:

public partial class reserve : Page
{
    public reserve()
    {
        InitializeComponent();
        List<String> hours = new List<string>();
        for (int i = 0; i < 24; i++)
            hours.Add(i.ToString());

        //I'm not sure how this should end up:
        Times.Remove(string hourofday);

        for (int i = 0; i < hours.Count; i++)

            dropdown_hours.Items.Add(i);
    }

    private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {
        //Get reference
        var reservationcal = sender as Calendar;

        //See if a date is chosen
        if (reservationcal.SelectedDate.HasValue)
        {
            DateTime SelectedDate = reservationcal.SelectedDate.Value;

            //Show available times for the chosen date
            DBHandler dbh = new DBHandler();
            //Here I am stuck:
            reservation r = dbh.GetReservedTimes                
        }
    }
}

我添加了SelectedDatesChanged =&#34; Calendar_SelectedDatesChanged&#34;到我的.xaml文件中的Calendar控件。

提前致谢 - 卡塔琳娜

2 个答案:

答案 0 :(得分:1)

Katharina,大部分功能都直接进入DateTime对象。而不是重新发明轮子,我会用它代替。在编程领域没有任何一种方法可以做任何事情,但有时,有更有效的方法来解决问题。 DateTimeTimeSpan个对象可以降低复杂性,提高可读性并最终实现可管理性。 根据数据库列的格式或数据类型“hourofday”,您可以按原样使用该值。否则,您可能需要将值解析为对象。 (见DateTime.Parse

以下是我为获得您要寻找的结果而采取的步骤:

  1. 确定业务需求。 (例如,你需要什么? 去做?你想要完成的是什么?)

  2. 汇总可在整个过程中使用的虚拟测试数据 设计过程。

  3. 使用虚拟测试数据,确定预期的内容 结果会是。这就是经常使用测试用例的原因。             (例如,您可以使用什么进行比较?)

  4. 确定整个过程的内容 相同的结果。             (例如。要从虚拟数据中获得完全相同的结果,您需要做什么?)

  5. 确定您上面使用的过程如何模块化 它是最细粒度的形式。             (例如,步骤中的过程。首先,您获得该值,然后执行该值。)

  6. 确定如何对这些步骤进行客观化或概括 进入容器类。 (例如,只有预订才能知道 它是否可见。它的父母应该只被允许询问。)

  7. 编码!

  8. 以下解决方案详细说明:     datetime对象已包含您应该需要的所有信息。 (例如年,月,日,小时,分钟,秒,毫秒等)     预订类对象将是非常基本的。它只包括与预订有关的信息。它不会知道它的父容器。     保留类基本上是保留的集合以及与处理其包含的保留相关的任何方法。         (例如,如果您遵循msdn命名约定和最佳实践,此类将继承ICollections并可能被称为'ReservationCollection')

    这种方法可以消除对整数数组的需要,因为它是多余的。

    var reserved = this._reservations.GetAllReserved();  //Returns a list of all existing reservations
    
    or
    
    var reserved = this._reservations.GetReservedOn(someDate);  //Return a list of all reservations on a given date
    

    我添加了一个非常简单的潜在解决方案演示,我已经详细评论过了。 (见下文)

    另外,只是一个注释。你在下面的评论中提到你想要0-23。一天24小时。索引0处的对象计为1个元素。所以0 - 23可以翻译成1 - 24。

        //Contains a collection of reservations and provides methods to get a 
        //reservation, get all reservations and set a new reservation
        Public class Reservations
        {
            private List<Reservation> _reservations;  //Collection of reservations
    
            public Reservations()
            {
                //Initialize properties
                this._reservations = new List<Reservation>();
            }
    
            //Returns all reservations
            public List<Reservation> GetAllReserved()
            {
                //If you are able to use linq, then use the following line
                //return this._reservations.Where(res => res.IsReserved() = true).ToList();
    
                //Otherwise use this
                List<Reservation> reserved = new List<Reservation>();
                foreach (var res in this._reservations)
                {
                    if (res.IsReserved())
                        reserved.Add(res);
                }
    
                return reserved;
            }
    
            //Returns all reservations on a given date
            public List<Reservation> GetReservedOnDate(DateTime reservedOn)
            {
                //If you are able to use linq, then use the following line
                //return this._reservations.Where(res => res.GetReservedOn() == reservedOn && res.IsReserved() = true).ToList();
    
                //Otherwise use this
                List<Reservation> reserved = new List<Reservation>();
                foreach (var res in this._reservations)
                {
                    if (res.GetReservedOn.Date == reservedOn.Date && res.IsReserved())
                        reserved.Add(res);
                }
    
                return reserved;
            }
    
            //Sets a reservation on a given date. Returns true if one is created, returns false if one can not.
            public bool SetReservation(DateTime reserveOn)
            {
                //Check to see if the date to reserve is already booked
                //If you are able to use linq, then use the following line
                if (_reservations.SingleOrDefault(res => res.GetReservedOn.Date == reservedOn.Date && 
                    res.GetReservedOn().Hour == reserveOn.Hour && 
                    res.IsReserved()) != null)
                {
                    return false;
                }
    
                //It has made it this far so no reservation exists at that time on that date, create a reservation
                this._reservations.Add(new Reservation(reserveOn));
    
                return true;
            }
        }
    
        //Contains the properties of a reservation. The reservation is ignorant of the hours as this provides
        //flexibility in the reservations collection handling class.
        public class Reservation
        {
            private DateTime _reserveOn;    //Date to reserve
            private bool _isReserved;       //Whether it is reserved
    
            //Overloaded constructor takes 1 parameter
            public Reservation(DateTime reserveOn)
            {
                //Initialize properties with assigned parameters
                this._reserveOn = reserveOn;  
                this._isReserved = true;
            }
    
            //Overloaded constructor takes 2 parameters
            public Reservation(DateTime reserveOn, bool isReserved)
                : this(reservedOn) //Calls the base constructor and passes 
                                               //in the reserved on parameter
            {
                //Initialize properties with assigned parameters 
                this._isReserved = true;
            }
    
            //Returns the date the reservation is on
            public GetReservedOn()
            {
                return this._reservedOn;
            }
    
            //Sets the reservation
            public void SetReservedOn(DateTime reserveOn)
            {
                this._reservedOn = reserveOn;
            }
    
            //Returns whether the date time has been reserved
            public bool GetIsReserved()
            {
                return this._isReserved;
            }
    
            //Set the reservation as reserved
            public void SetIsReserved(bool isReserved)
            {
                this._isReserved = isReserved;
            }
        }
    

    要了解如何使用以下脚本:查看以下链接。

    Binding a collection of objects to a control

答案 1 :(得分:0)

它不是这个问题的完全解决方案,但您可以在数据库上将数据类型转换为Text并在程序whit calender中控制此单元格。很容易将它们结合起来。你的方式比这种方式更复杂。