samedi 4 mars 2017

Adding multiple rows to database based on the difference between dates

I want to check for any clashes that exist between the start date to the end date of the potential class that I am inserting. I have a ClassSchedule table which contains the time from 8:00:00 - 18:00:00 (1-11 rows). StartTime and EndTime are both Foreign Keys (FK) of the ClassScheduleId.

If a Class already exists in the database from 02/03/2017 09:00:00 - 20/03/2017 10:00:00, the StartTime is 2 (09:00:00) and the EndTime is 4 (10:00:00).

Note: When the user selects a start and end date the Time value is set to 00:00:00 by default, it is only when they insert it does the time get set correctly.

However, if I want to add a class 01/03/17 00:00:00 - 10/03/17 00:00:00 , the StartTime is set to 3 (10:00:00) and the EndTime is 5 (11:00:00). As you can see this class overlaps on one some of the days already in the database from the first example.

I want to create a loop which will recognise that there is a clash by checking from the start date up to the end end date for a particular RomeCodeId. Additionally checking that the new class does not fall between the StartTime and EndTime for any record in the database. If there is even an overlap of 1 day it should detect a clash.

This is my attempt of a For loop, which returns "There is clashes" if the user enters the exact same class into the database.

 protected void check_clash(object sender, EventArgs e)
    {
            DateTime startdatedata = Convert.ToDateTime(txtstartdate.Text);
            DateTime enddatedata = Convert.ToDateTime(txtenddate.Text);
            int classtypedata = Convert.ToInt32(ddlClassType.Text);
            int roomcodedata = Convert.ToInt32(ddlRoomCode.Text);
            int starttimedata = Convert.ToInt32(ddlStartClassTime.Text);
            int endtimedata = Convert.ToInt32(ddlEndClassTime.Text);

            startdatedata = DateTime.Parse(txtstartdate.Text).Date;
            enddatedata = DateTime.Parse(txtenddate.Text).Date; 

        for  (int i = starttimedata; i <= endtimedata; i++)
        {
             string connectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

             SqlConnection myConnection = new SqlConnection(connectionString);

             myConnection.Open();


             string query = "SELECT * FROM Class WHERE (StartTime >=@startdatedata) AND ( EndTime <@enddatedata ) AND (ClassScheduleStartTimeId = @starttimedata) AND (ClassScheduleEndTimeId = @endtimedata) AND RoomCodeId = @roomcodedata";
             SqlCommand myCommand = new SqlCommand(query, myConnection);

             myCommand.Parameters.AddWithValue("@startdatedata", startdatedata);
             myCommand.Parameters.AddWithValue("@enddatedata", enddatedata);
             myCommand.Parameters.AddWithValue("@roomcodedata", roomcodedata);
             myCommand.Parameters.AddWithValue("@starttimedata", starttimedata);
             myCommand.Parameters.AddWithValue("@endtimedata", endtimedata);
             myCommand.Parameters.AddWithValue("@i", i);
             SqlDataReader rdr = myCommand.ExecuteReader();

            if (rdr.HasRows)
            {
                Feedback.Text = "There is clashes";
            }
            else 
            {
              Feedback.Text = "There is no clashes";
            }

            myConnection.Close();
        }
    }

Aucun commentaire:

Enregistrer un commentaire