Global.asax 定时更新数据的问题 用vs2005想实现程序每天下午3-5点和晚上8:00-9:00每隔10秒往数据库里插入一条数据。不知道用 Global.asax 能不能实现。 Global.asax 该怎么写?请高手给出详细代码!最好有备注。万分感谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用WinForm或者Console都可以实现,犯得着写在Global.asax里面吗? 你的网站应该是部署windows机器上的吧,那可以写一个windows服务,设置为自动启动。不用别人浏览你的电脑,只要你的网站在运行那就可以了。如果非要在Global.asax.cs里面写的话,那么你可以在application_start中写一个方法,最好起一个线程去做这件事。但是这个有一个前体,就是部署好以后你需要访问一下网站激活application_start事件。 Global.asax中加一个线程定时器是可以实现的,不过用Windows服务可能更好些,放在单独的应用程序(进程)中 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); }希望你能够理解 love_99 谢谢!我用的是虚拟主机,还是用您前面说的windows服务。Global.asax.cs 能实现每天定时更行吗?就是在我浏览网页后?或者应该浏览那个页面? 说真的如果只是插入数据库的话,我觉得根本不用代码段去处理。。 直接SQLSERVER 的作业就可以了 其实更简单的做法是写个CLR SP(.NET编写的存储过程),然后建立数据库的代理作业定时执行这个存储过程去抓取数据。 看了楼上的话如果要求 没人访问的话 也定时执行的话 在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的方法 }} 楼主 这种需求都是做成window服务的 虚拟机你能做的是太少了。Global.asax.cs里面写定时器或者起线程定时跑没问题的。你只需要访问任意一个aspx页面就可以了 在Global.asax.cs里面实现,然后再在本地机器上做个Winform程序,一直运行这个Winform程序,定时刷新你的网站? -_-|| 看我的 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(); } 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(); } wpf弹出窗体的问题 在 windows xp 下如何安装 Oracle 11g 线程问题 ListView拖动高亮+TreeView多选 如何将结构体写入到文件中去? DataGrid 行合并的问题? OleDb和OracleClient效率有多大差别? 如何将Label中的文字虽Label的宽度变扁或变长 如何通过程序把dataTable中某指定行列值清空? 有难,请出手.... C#做地图相关的东西。 提高数据库的数据处理速度
如果非要在Global.asax.cs里面写的话,那么你可以在application_start中写一个方法,最好起一个线程去做这件事。但是这个有一个前体,就是部署好以后你需要访问一下网站激活application_start事件。
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);
}
希望你能够理解
说真的如果只是插入数据库的话,我觉得根本不用代码段去处理。。 直接SQLSERVER 的作业就可以了
如果要求 没人访问的话 也定时执行的话 在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的方法
}
}
-_-||
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();
}
{
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();
}