用vs2005想实现程序每天下午3-5点和晚上8:00-9:00每隔10秒往数据库里插入一条数据。不知道用 Global.asax 能不能实现。
 Global.asax 该怎么写?请高手给出详细代码!最好有备注。万分感谢!

解决方案 »

  1.   

    用WinForm或者Console都可以实现,犯得着写在Global.asax里面吗?
      

  2.   

    你的网站应该是部署windows机器上的吧,那可以写一个windows服务,设置为自动启动。不用别人浏览你的电脑,只要你的网站在运行那就可以了。
    如果非要在Global.asax.cs里面写的话,那么你可以在application_start中写一个方法,最好起一个线程去做这件事。但是这个有一个前体,就是部署好以后你需要访问一下网站激活application_start事件。
      

  3.   

    Global.asax中加一个线程定时器是可以实现的,不过用Windows服务可能更好些,放在单独的应用程序(进程)中
      

  4.   


    protected void Application_Start()
            {
                //新启动一个线程。
                Thread currentThread = new Thread(new ThreadStart(InsertDB));
                currentThread.Start();
            }
            //具体操作方法
            void InsertDB()
            {
                int hour = DateTime.Now.Hour;
                if(hour>8 && hour<9)
                {
                    //写入数据
                }
                else if (hour > 15 && hour < 17)
                {
                    //写入数据
                }
                System.Threading.Thread.Sleep(10000);
            }
    希望你能够理解
      

  5.   

    love_99  谢谢!我用的是虚拟主机,还是用您前面说的windows服务。Global.asax.cs 能实现每天定时更行吗?就是在我浏览网页后?或者应该浏览那个页面?
      

  6.   


    说真的如果只是插入数据库的话,我觉得根本不用代码段去处理。。 直接SQLSERVER 的作业就可以了
      

  7.   

    其实更简单的做法是写个CLR SP(.NET编写的存储过程),然后建立数据库的代理作业定时执行这个存储过程去抓取数据。
      

  8.   

    看了楼上的话
    如果要求 没人访问的话 也定时执行的话 在global.asax里面写定时器刷新的话是没有用的 
    因为global.asax没有人访问的话 是不执行的 
    而如果楼主退而其次 只要有人访问的话 那是可以的 
    你可以在global.asax里面写个定时器 设置个定时间隔 在定时器的事件里写上时间判断 如果符合要求则执行
    比如:void Application_Start(object sender, EventArgs e) 
    {
         //在应用程序启动时运行的代码
          System.Timers.Timer aTimer = new System.Timers.Timer();
         int seconds = 60;
         aTimer.Elapsed += new ElapsedEventHandler(TimedEvent);
         aTimer.Interval = seconds * 1000;//一分钟
         aTimer.Enabled = true;
    }
    void TimedEvent(object source, ElapsedEventArgs e)
    {
        string appStartTime = ConfigurationManager.AppSettings["StartTime"];
        string theNowTime = DateTime.Now.ToString("HH:mm");
        if (theNowTime == appStartTime)
        {
           Method();//dosomething的方法
        }
    }
      

  9.   

    楼主  这种需求都是做成window服务的
      

  10.   

    虚拟机你能做的是太少了。Global.asax.cs里面写定时器或者起线程定时跑没问题的。你只需要访问任意一个aspx页面就可以了
      

  11.   

    在Global.asax.cs里面实现,然后再在本地机器上做个Winform程序,一直运行这个Winform程序,定时刷新你的网站?
     -_-||
      

  12.   

    看我的
        private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            aaabbb.RecieveXcd rxcd=new aaabbb.RecieveXcd();/////自动经ftp下载行程的
    aaabbb.EXcdParse.init(true,200 );

            WebResponse response1 = WebRequest.Create("http://localhost:8080/").GetResponse();///访问自已,以免停死
                StreamReader read=new StreamReader( response1.GetResponseStream() );
                //Response.Write(read.ReadToEnd());
    read.ReadToEnd();
                //Response.Write( response1.GetResponseStream().Read(new byte[],0,, .ReadByte().ToString() );
                read.Dispose();
                read.Close();
            DB.wlog("Global定时","");                                                                                 
              //rt.run();     
        }
      

  13.   

        private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {
            aaabbb.RecieveXcd rxcd=new aaabbb.RecieveXcd();/////自动经ftp下载行程的
    aaabbb.EXcdParse.init(true,200 );

            WebResponse response1 = WebRequest.Create("http://localhost:8080/").GetResponse();///访问自已,以免停死
                StreamReader read=new StreamReader( response1.GetResponseStream() );
                //Response.Write(read.ReadToEnd());
    read.ReadToEnd();
                //Response.Write( response1.GetResponseStream().Read(new byte[],0,, .ReadByte().ToString() );
                read.Dispose();
                read.Close();
            DB.wlog("Global定时","");                                                                                 
            //aaabbb.EXcdParse.i_parsenum.ToString();
     //aaabbb.CountNum.run();
            //aaabbb.Routine rt = aaabbb.Routine.GetRoutine();  短信的
            //rt.run();     
        }