0

下面你会看到我的网站日历的代码,问题是这段代码需要大约 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;
    }

我知道这是一堆代码,但我不知道如何减少它。

4

4 回答 4

2

每个 page_load 都会导致 cca 30 数据库查询 - 那是很多开销,请尝试修改它,以便您只需要与数据库交谈一次,如果有的话。除此之外,获取 .NET 分析器并查看您花费最多时间的地方。

于 2012-06-05T20:44:12.017 回答
2

您可以通过获取整个间隔的所有事件来提高性能,而不是为每个日期执行一个查询。鉴于您当前的数据模型很容易更改,您似乎希望在一个月内完成所有事件。

如果您确实选择每天执行一个查询,请SqlCommand通过添加SqlParameters来重复使用相同的查询,而不是重新构建一个全新的查询 28-31 次。

于 2012-06-05T20:44:37.013 回答
1

基本的字符串优化会产生很大的不同。在您string +=使用字符串生成器的地方。然后当所有计算完成后,使用 .ToString() 方法将 StringBuilder 转换为字符串。

请参阅 MSDN StringBuilder 类

还将代码替换string + string + string为 AppendFormat 方法

于 2012-06-05T20:40:36.613 回答
1

1)用途:

day = date.Day;

代替:

day = int.Parse(date.ToString().Substring(0, 2));

2) 将此长样式放入样式表/CSS 中,并改用短 class='name':

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);

3) 您每天调用 Begivenhed_By_Date 两次:

1)     count = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection).Count;
2)    foreach (Begivenheder b in Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection))
could be:
+)    List<Begivenheder> hedByDate = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection);
1.x   count = hedByDate.Count;
2.x   foreach (Begivenheder b in hedByDate, connection))

4) 至少先使用 StringBuilder 连接 lbl_Month.Text += 字符串,然后最后分配。

5) 研究使用中继器控件,因此您不必像这样在代码中乱扔 HTML。

6) 更改它,以便您只执行一次 SQL 查询,而不是多次。

于 2012-06-05T21:06:44.083 回答