全局应用程序Global.asax  是什么时候开始执行? 我写了个小例子,执行的是每2秒对数据库进行一次插入操作,
但是我看不到执行效果,编译器也不报错,数据库表里没有任何变化,这个程序我该怎么测啊,问题在哪?Global.asax中的代码如下:<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %><script runat="server">    void Application_Start(object sender, EventArgs e) 
    {
        System.Timers.Timer aTimer = new System.Timers.Timer();        aTimer.Elapsed += new ElapsedEventHandler(OnTimer);        aTimer.Interval = 2000;
        aTimer.Enabled = true;
        aTimer.Start();    }    public static void OnTimer(object sourse, ElapsedEventArgs e)
    {
        string connectionString = "Data Source=192.123.2.13;Initial Catalog=hhLuck;Persist Security Info=True;User ID=glluck_dbo;Password=xxxxx]o";
        string str = "insert into platinumCard_repayment_logs(account, repay_num, repayDate) values('11',20,getdate())";
        System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(connectionString);
        System.Data.SqlClient.SqlCommand sqlCommand1;
        sqlConnection1.Open();
        sqlCommand1 = new System.Data.SqlClient.SqlCommand(str, sqlConnection1);
        sqlCommand1.ExecuteNonQuery();        sqlConnection1.Close();
        Console.WriteLine("11111");    } 
    
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码    }    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。    }
       
</script>

解决方案 »

  1.   

    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Timers" %><script runat="server">System.Timers.Timer aTimer = new System.Timers.Timer();    void Application_Start(object sender, EventArgs e)
        {
            aTimer.Elapsed += new ElapsedEventHandler(OnTimer);
            aTimer.Interval = 2000;
            aTimer.Enabled = true;
            aTimer.Start();
        }    public static void OnTimer(object sourse, ElapsedEventArgs e)
        {
            string connectionString = "Data Source=192.123.2.13;Initial Catalog=hhLuck;Persist Security Info=True;User ID=glluck_dbo;Password=xxxxx]o";
            string str = "insert into platinumCard_repayment_logs(account, repay_num, repayDate) values('11',20,getdate())";
            System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(connectionString);
            System.Data.SqlClient.SqlCommand sqlCommand1;
            try{
            sqlConnection1.Open();
            sqlCommand1 = new System.Data.SqlClient.SqlCommand(str, sqlConnection1);
            sqlCommand1.ExecuteNonQuery();
            }
            catch(Exception e) {
                   //log error
             }        sqlConnection1.Close();
            Console.WriteLine("11111");    }
       
       
        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码    }
           
        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码    }    void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
            // 或 SQLServer,则不会引发该事件。    }
         
    </script>
      

  2.   

    ·执行的是每2秒对数据库进行一次插入操作·--golbal是在aspx加载前运行,而且遵循页面生命周期,你这种要求用ajax实现吧,System.Timers.Timer BS中无效。
      

  3.   

    找了一些资料 说这个 Global.asax中加个timer可以实现 不开页面 每天定时对数据库进行一系列操作,楼上这位又说 Timer在BS中无效~! 到底谁的对~!无解了啊?