我在Global.asax文件里:using System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.SessionState; using System.Data; namespace WebTest { /// <summary> /// Global 的摘要说明。 /// </summary> public class Global : System.Web.HttpApplication { private System.Timers.Timer timer1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; public Global() { InitializeComponent(); }
用
<table border=1><tr><td id=showtime></td></tr></table>
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD><BODY>
<table border=1><tr><td id=showtime></td></tr></table>
</BODY>
</HTML><script>
setInterval("update()",1000);
function update()
{
var mydate=new Date();
document.all.showtime.innerText = mydate;
}
</script>
因为他是应用程序级。using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;namespace ZZ
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
private System.Timers.Timer timer1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
this.timer1.Start();
}
protected void Session_Start(Object sender, EventArgs e)
{ } protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{ } protected void Application_End(Object sender, EventArgs e)
{ }
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit(); }
#endregion private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//做楼主想做的。
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Data;
namespace WebTest
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
private System.Timers.Timer timer1; /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{ }
protected void Session_Start(Object sender, EventArgs e)
{ } protected void Application_BeginRequest(Object sender, EventArgs e)
{ } protected void Application_EndRequest(Object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{ } protected void Application_Error(Object sender, EventArgs e)
{ } protected void Session_End(Object sender, EventArgs e)
{ } protected void Application_End(Object sender, EventArgs e)
{ }
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();
sqlConn.ConnectionString = "Data source=127.0.0.1;User ID=sa;Password=123;Initial Catalog =onlinezx";
sqlConn.Open();
System.Data.SqlClient.SqlCommand sqlCom = new System.Data.SqlClient.SqlCommand("INSERT INTO test(name, addTime) VALUES ('oldjacky', '"+ System.DateTime.Now +"')");
sqlCom.CommandType = CommandType.Text;
sqlCom.Connection = sqlConn;
sqlCom.ExecuteNonQuery();
sqlConn.Close();
} #endregion
}
}
数据库还是不能一秒更新一次,加上条记录(声明:我的SQL调试是正确的)