ASP.NET页面崩溃与MySql连接

时间:2013-03-10 17:00:49

标签: c# asp.net mysql monodevelop

我正在尝试编写一个信息中心,其中包含有关网站点击次数和人们来自哪里的信息等。

我写了以下页面:

<%@ Page Language="C#" Inherits="Daschboard.Board" MasterPageFile="~/site.master" %>        
<asp:Content id="main" runat="server" ContentPlaceHolderID="main">
<asp:UpdatePanel runat="server" id="updateableWidgets">
    <ContentTemplate>
        <asp:ContentPlaceHolder id="WidgetsTable">
        </asp:ContentPlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

后面的代码如下:

using Widgets;
using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace Daschboard
{
public partial class Board : System.Web.UI.Page
{
    protected List<IWidget> widgets;
    private MySqlConnection db = new MySqlConnection (ConfigurationManager.AppSettings ["mySql"]);

    protected void Page_Load(object sender, EventArgs e)
    {
        widgets = new List<IWidget>();
        BuildWidgtTable();
        var WidgetThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.InitWidgets));
        WidgetThread.Start();
    }

    private string BuildWidgtTable ()
    {
        StringBuilder sb = new StringBuilder ();
        db.Open ();
        var tableName = ConfigurationManager.AppSettings ["DatabaseSiteName"];
        var query = "SELECT * FROM " + tableName + ", Widget WHERE " + tableName + ".WidgetID = Widget.WidgetID";
        var command = new MySqlCommand (query, db);
        var result = command.ExecuteReader ();
        while (result.Read()) {
            widgets.Add(getWidget(result.GetString(4)));
        }
        db.Close ();

        sb.Append("<table>\n");

        sb.Append("</table>");

        result.Close();
        return sb.ToString();
    }

    private void InitWidgets ()
    {
        db.Open();
        foreach (var widget in widgets) {
            var siteId = ConfigurationManager.AppSettings["siteID"];
            var query = "SELECT * FROM Values, Widget WHERE       Widget.WidgetID = Values.WidgetID AND SiteID = " + siteId;
            var command = new MySqlCommand(query, db);
            var result = command.ExecuteReader();
            while(result.Read()){
                Console.WriteLine(result);
            }
            result.Close();
            widget.Init ("1");
        }
        db.Close();
    }

    private IWidget getWidget (string fileName)
    {
        IWidget widget = null;
        switch (fileName) {
        case "PageHits":
            widget = new PageHits();
            break;
        case "SiteHits":
            widget = new SiteHits();
            break;
        }
        return widget;
    }
}
}

当我跑步时,出了问题;调试器在没有错误的情况下停止工作。 它确实从数据库加载小部件,但它不从数据库中获取值。

我在Mac上使用Monodevelop。

1 个答案:

答案 0 :(得分:0)

您正在一个单独的线程(InitWidgets方法)中运行窗口小部件初始化方法。此方法执行时,页面可能已完成渲染。如果您想在ASP.NET中使用异步操作,我建议您阅读MSDN上的Asynchronous Pages。我们的想法是在您的Page指令(Async="true")中设置<%@ Page Async="true" ... %>,然后在网络表单上使用RegisterAsyncTask方法,将WebForm标记为异步。