以下に、ウェブサイトのカレンダーのコードを示します。問題は、このコードの読み込みに約 8 ~ 10 秒かかることです。読み込み時間を最小限に抑える方法を見つけた方がいらっしゃいましたら、よろしくお願いします。
public static IEnumerable<DateTime> AllDatesInMonth(int year, int month)
{
foreach (var day in Enumerable.Range(1, DateTime.DaysInMonth(year, month)))
{
yield return new DateTime(year, month, day);
}
}
public void ForeachDayInMonth(int year, int month, SqlConnection connection)
{
int day;
int count;
double divHeight;
lbl_Month.Text += "<table class=\"Month\">";
foreach (DateTime date in AllDatesInMonth(year, month))
{
day = int.Parse(date.ToString().Substring(0, 2));
count = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection).Count; // Creates a sql select statement
lbl_Month.Text += "<tr>" +
"<td style=\"height: 30px; width: 70px;";
if (date.Date == DateTime.Today)
{
lbl_Month.Text += "-webkit-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" +
"-moz-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" +
"box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);";
}
lbl_Month.Text += "\">";
if (count != 0)
{
divHeight = 100 / count;
foreach (Begivenheder b in Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection)) // creates a sql select statement
{
lbl_Month.Text += "<div style=\"background-color: " + b.begivenhed.type.TypeFarve + "; height: " + divHeight + "%;\">" +
"<a href=\"KalenderEvent.aspx?Event=" + b.begivenhed.ID + "\">";
if (b.begivenhed.Navn.Length > 9)
{
lbl_Month.Text += b.begivenhed.Navn.Remove(9) + "...";
}
else
{
lbl_Month.Text += b.begivenhed.Navn;
}
lbl_Month.Text += "</a>" +
"</div>";
}
}
lbl_Month.Text += "</td>" +
"</tr>";
}
lbl_Month.Text += "</table>";
}
protected void Page_Load(object sender, EventArgs e)
{
string year;
if (Request.QueryString["Year"] == null)
{
year = DateTime.Today.Year.ToString();
}
else
{
year = Request.QueryString["Year"];
}
Page.Title += " - Kalender " + year;
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]);
connection.Open();
try
{
Year(int.Parse(year), connection);
}
finally
{
connection.Close();
}
}
private void Year(int year, SqlConnection connection)
{
string thisYear = year.ToString();
int lastYear = year - 1;
int nextYear = year + 1;
lbl_Year.Text = "<div>" +
"<a href=\"Kalender.aspx?Year=" + lastYear.ToString() + "\" id=\"LastYear\"></a>" +
"</div>" +
"<div>" +
thisYear +
"</div>" +
"<div>" +
"<a href=\"Kalender.aspx?Year=" + nextYear.ToString() + "\" id=\"NextYear\"></a>" +
"</div>";
lbl_Month.Text = "<table>" +
"<tr>" +
"<td>";
// Dage
lbl_Month.Text += "<p>" +
"</p>" +
"<br />" +
"<table>";
for (int i = 1; i <= 31; i++)
{
lbl_Month.Text += "<tr>" +
"<td style=\"height: 30px;\">" +
"<p>" +
i.ToString() +
"</p>" +
"</td>" +
"</tr>";
}
lbl_Month.Text += "</table>" +
"</td>";
//Januar (repeats 12 times, ones for each month)
lbl_Month.Text += "<td>" +
"<p>" +
"Januar" +
"</p>";
ForeachDayInMonth(int.Parse(thisYear), 1, connection);
lbl_Month.Text += "</td>";
lbl_Month.Text += "</tr>" +
"</table>";
}
「Begivenheder.Get_Begivenhed_By_Date」コードはこちらです。
public static List<Begivenheder> Get_Begivenhed_By_Date(int år, int måned, int dag, SqlConnection connection)
{
List<Begivenheder> result = new List<Begivenheder>();
using (var command = new SqlCommand("Select ID, FK_Begivenhed_ID from Begivenhed_Datoer where Dag=" + dag + " and Måned=" + måned + " and År=" + år, connection))
{
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Begivenheder b = new Begivenheder();
b.Dato_ID = reader.GetInt32(0);
b.ID = reader.GetInt32(1);
b.Dato_Day = dag;
b.Dato_Month = måned;
b.Dato_Year = år;
result.Add(b);
}
}
finally
{
reader.Close();
}
foreach (Begivenheder b in result)
{
b.begivenhed = Begivenheder.Get_Begivenhed_By_ID(b.ID, connection);
}
}
return result;
}
私はそれがコードの束であることを知っていますが、それを減らす方法がわかりません。