当用户向系统添加学校课程时,他们会选择课程的开始和结束日期。我已经研究了如何计算它的天数并将其存储在'TotalDayNumber'列中的数据库表'Class'中。
目前,当用户添加一个类时,它只会在数据库中插入一行,但我想为每一天'TotalDayNumber'添加相同数量的行。
例如,如果开始日期是01/03/2017 - 15/03/17 - 总天数为14,那么我想创建总共14行来代表每一天。 '下面是我的INSERT查询。我相信我需要一个IF声明,但我不知道如何开始它。
protected void insertbutton_Click(object sender, EventArgs e)
{
int? recurrencedata = Convert.ToInt32(ddlRecurrence.Text);
if (recurrencedata == 1)
{
int moduledata = Convert.ToInt32(ddlModule.Text);
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;
TimeSpan totaldays = enddatedata - startdatedata;
// This is rounding the TimeSpan to the day value only
string totalday = ((int)Math.Round(totaldays.TotalDays, MidpointRounding.AwayFromZero)).ToString();
string DefaultConnection = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection myConnection = new SqlConnection(DefaultConnection);
myConnection.Open();
string query = "INSERT INTO Class (ModuleId, ClassTypeId, ClassScheduleStartTimeId, ClassScheduleEndTimeId, RoomCodeId, StartTime, EndTime, RecurrenceId, TotalDayNumber) VALUES ( @moduledata, @classtypedata, @starttimedata, @endtimedata, @roomcodedata, @startdatedata, @enddatedata, @recurrencedata, @totaldaynumberdata)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@moduledata", moduledata);
myCommand.Parameters.AddWithValue("@classtypedata", classtypedata);
myCommand.Parameters.AddWithValue("@startdatedata", startdatedata);
myCommand.Parameters.AddWithValue("@enddatedata", enddatedata);
myCommand.Parameters.AddWithValue("@starttimedata", starttimedata);
myCommand.Parameters.AddWithValue("@roomcodedata", roomcodedata);
myCommand.Parameters.AddWithValue("@endtimedata", endtimedata);
myCommand.Parameters.AddWithValue("@recurrencedata", recurrencedata);
myCommand.Parameters.AddWithValue("@totaldaynumberdata", totalday);
myCommand.ExecuteNonQuery();
myConnection.Close();
SuccessPanel.Visible = true;
}
}
答案 0 :(得分:0)
将总天数设为int
,然后添加带有for
循环的插入命令,如下所示
// add for loop
for(int i =0; i <= totalday; i++)
{
myConnection.Open();
string query = "INSERT INTO Class (ModuleId, ClassTypeId, ClassScheduleStartTimeId, ClassScheduleEndTimeId, RoomCodeId, StartTime, EndTime, RecurrenceId, TotalDayNumber) VALUES ( @moduledata, @classtypedata, @starttimedata, @endtimedata, @roomcodedata, @startdatedata, @enddatedata, @recurrencedata, @totaldaynumberdata)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@moduledata", moduledata);
myCommand.Parameters.AddWithValue("@classtypedata", classtypedata);
myCommand.Parameters.AddWithValue("@startdatedata", startdatedata);
myCommand.Parameters.AddWithValue("@enddatedata", enddatedata);
myCommand.Parameters.AddWithValue("@starttimedata", starttimedata);
myCommand.Parameters.AddWithValue("@roomcodedata", roomcodedata);
myCommand.Parameters.AddWithValue("@endtimedata", endtimedata);
myCommand.Parameters.AddWithValue("@recurrencedata", recurrencedata);
myCommand.Parameters.AddWithValue("@totaldaynumberdata", totalday);
myCommand.ExecuteNonQuery();
myConnection.Close();
}