访客柜台 [英] Visitor counter
本文介绍了访客柜台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我已经使用asp.net 2.0开发了一个访客计数器,但是我认为这是不必要的每天增加的.例如,如果访问者人数为20,则显示为60-70.请解决此问题,代码是写在global.asax文件中的,如下所示:
Sub Application_Start( ByVal 发件人 As 对象, ByVal e As EventArgs)
' 在应用程序启动时运行的代码
昏暗结果结果 整数
Dim conn As OleDbConnection
Dim comm As OleDbCommand
Dim strConnection As String
strConnection = " + Server.MapPath( " )
conn = 新建 OleDbConnection(strConnection)
comm = 新建 OleDbCommand(" ,conn)
conn.Open()
结果= CType (comm.ExecuteScalar(),字符串)
Application(" )=结果
' Dim总计为字符串= Application("counter")
conn.Close()
结束 子
子 Session_Start( ByVal 发件人 As 对象, ByVal e As EventArgs)
' 启动新会话时运行的代码
Application.Lock()
Application(" )= Application(" 计数器")+ ' Application.UnLock()
Dim conn1 As OleDbConnection
Dim comm1 As OleDbCommand
Dim strConnection1 As 字符串
strConnection1 = " + Server.MapPath( " )
conn1 = 新建 OleDbConnection(strConnection1)
comm1 = 新建 OleDbCommand(" & Application( 计数器")& '其中visitorname ='naresh'",conn1)
conn1.Open()
comm1.ExecuteNonQuery()
conn1.Close()
Application.UnLock()
结束 子
解决方案
尝试:
1)设置Web.Config文件:添加您的断点"(在Application对象将其计数转储到数据库之前的访问量
2)Global.asax:
Sub Application_Start(发送方 As Object ,例如 As EventArgs) Application(" )= Visit.GetTotalCount() 结束 子 Sub Session_Start(发送方 As 对象,e As EventArgs) Application(" )= CInt (应用程序(" ))+ 1 跨度> Application(" )= CInt (Application(" ))+ 1 跨度> 如果 CInt (应用程序(" TotalUsers")))> = " MAX")) CInt (Application(" ))) Application(" )= 0 结束 如果 结束 子
3)创建一个新的Class文件(Visits.vb):
公共 课程访问 公共 共享 Sub 计数(访问 As 整数) Dim connectionString As String = " Dim myConnection As 新建 SqlConnection(connectionString) Dim myCommand As 新建 SqlCommand(" ,myConnection) myCommand.Parameters.Add(新建 SqlParameter(" ,访问)) 尝试 myCommand.Connection.Open() myCommand.ExecuteNonQuery() 捕获 最后 如果 myConnection.State = ConnectionState.Open 然后 myConnection.Close 结束 尝试 结束 子 结束 类
完成了!
看看这篇文章:
在Asp.net中实现点击计数器 [ ^ ]
Hi
I have develop a visitor counter using asp.net 2.0, but i think it is unnecessary incremented day by day. for example if the visitor count is 20 it shows around 60-70. please solve the problem, the code is written in global.asax file, given below:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Dim result As Integer
Dim conn As OleDbConnection
Dim comm As OleDbCommand
Dim strConnection As String
strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("database/counter.mdb")
conn = New OleDbConnection(strConnection)
comm = New OleDbCommand("SELECT visitor FROM [counter]where visitorname='naresh'", conn)
conn.Open()
result = CType(comm.ExecuteScalar(), String)
Application("counter") = result
'Dim total As String = Application("counter")
conn.Close()
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Application.Lock()
Application("counter") = Application("counter") + 1
'Application.UnLock()
Dim conn1 As OleDbConnection
Dim comm1 As OleDbCommand
Dim strConnection1 As String
strConnection1 = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("database/counter.mdb")
conn1 = New OleDbConnection(strConnection1)
comm1 = New OleDbCommand("UPDATE [counter] SET visitor = ' " & Application("counter") & " ' where visitorname='naresh' ", conn1)
conn1.Open()
comm1.ExecuteNonQuery()
conn1.Close()
Application.UnLock()
End Sub
解决方案
Try:
1) Setup Web.Config file: Add your "breakpoint" (the amount of visits before the Application object dumps it''s count to the Database
2) Global.asax:
Sub Application_Start(sender As Object, e As EventArgs) Application("RunningCount") = Visit.GetTotalCount() End Sub Sub Session_Start(sender As Object, e As EventArgs) Application("TotalUsers") = CInt(Application("TotalUsers")) + 1 Application("RunningCount") = CInt(Application("RunningCount")) + 1 If CInt(Application("TotalUsers")) >= CInt(ConfigurationSettings.AppSettings("MAX")) Then Visit.Count(CInt(Application("TotalUsers"))) Application("TotalUsers") = 0 End If End Sub
3) Create a new Class file (Visits.vb):
Public Class Visits Public Shared Sub Count(visits As Integer) Dim connectionString As String = "workstation id=GEEK;packet size=4096;integrated security=SSPI;initial catalog=MyDatabase;persist security info=False" Dim myConnection As New SqlConnection(connectionString) Dim myCommand As New SqlCommand("UPDATE Visitor SET TotalVisits = @visits + TotalVisits WHERE VisitID = 1", myConnection) myCommand.Parameters.Add(New SqlParameter("@visits", visits)) Try myCommand.Connection.Open() myCommand.ExecuteNonQuery() Catch Finally if myConnection.State = ConnectionState.Open Then myConnection.Close End Try End Sub End Class
Its done!
Have a look at this article:
Implementing Hit Counter in Asp.net[^]
这篇关于访客柜台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文