'以下导入名称空间,注意这里是imports,不是import。
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls '因为使用web控件,所以导入
Imports System.Data
Imports System.Data.OleDb
Imports System.DateTime
Imports System.Configuration '因为使用Now(),所以要导入
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Web.UI.HtmlControls
Public Class Global: Inherits HttpApplication
protected sub Application_Start(sender As Object,e As EventArgs)
Application("strcon")="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("##2014##123###/##sffr20#11#")
DeleteAllUser()
End sub
Public Sub DeleteAllUser()
dim conn As New OleDbConnection(Application("strcon").ToString())
Dim strSq = "Delete from OnlineUsers1 Where UserId<>'大家'"
Dim cmd As New OleDbCommand(strSq, conn)
conn.Open()
cmd.ExecuteNonQuery() '删除记录
conn.Close()
End Sub
protected Sub Session_onStart(sender As Object,E As EventArgs)
'每一个客户登录时触发该事件
dim conn As New OleDbConnection(Application("strcon").ToString()) '使用了私有变量的值
'建立Command对象,注意这里使用了含有参数的SQL语句
dim IP as string
ip= Request.ServerVariables("REMOTE_ADDR")
Response.cookies("IPAddress").value=""
'Response.Cookies("ip").Expires = DateTime.Now.AddDays(1)
Dim Sql1 As String = "Select Count(*) As total From OnlineUsers Where ip='" & IP & "' and txtday = #" & today() & "#"
Dim cmd1 As New OleDbCommand(Sql1, conn) '建立Command对象
conn.Open()
Dim intTotal As Integer = cmd1.ExecuteScalar()
If intTotal > 0 Then
conn.Close() '该条件表示该用户名存在
Dim strSq As String = "Insert Into OnlineUsers(SessionId,UserId,LogInTime,IsOnline,txtday) Values('" & Session.SessionId & "','过客',#" & Now() & "#,True,#" & today() & "#)"
Dim cmd As New OleDbCommand(strSq, conn)
'下面执行操作
conn.open()
cmd.ExecuteNonQuery()
else
conn.close()
dim txta = datetime.today.month
Dim Sql2 As String = "Insert Into OnlineUsers(SessionId,UserId,LogInTime,IsOnline,ip,txtday,txtmonth) Values('" & Session.SessionId & "','过客',#" & Now() & "#,True,'" & IP & "',#" & today() & "#,'" & txta & "')"
Dim cmd2 As New OleDbCommand(Sql2, conn)
'下面执行操作
conn.open()
cmd2.ExecuteNonQuery()
conn.close()
end if
End Sub
sub Application_BeginRequest(sender As Object,e As EventArgs)
End Sub
Sub Application_AuthenticateRequest(sender As Object, e As EventArgs)
End Sub
Sub Application_Error(sender As Object,e As EventArgs)
End Sub
protected Sub Session_OnEnd(Sender As Object, E As EventArgs)
dim conn As New OleDbConnection(Application("strcon").ToString())
'每一个客户退出登录时触发该事件
Dim strSql As String = "Update OnlineUsers Set IsOnline=False,LogOutTime=#" & Now() & "# Where SessionId='" & Session.SessionId & "' And IsOnline=True"
Dim cmd As New OleDbCommand(strSql, conn)
'下面执行操作
conn.open()
cmd.ExecuteNonQuery()
conn.close()
DeleteUser(Session("UserId"))
UserGo(Session("UserId"), Session("IP"), Session("ChatRoom"))
End Sub
Public Sub DeleteUser(strUserId As String)
dim conn As New OleDbConnection(Application("strcon").ToString())
Dim strSql = "Delete from OnlineUsers1 Where UserId='" & strUserId & "'"
Dim cmd As New OleDbCommand(strSql, conn)
conn.Open()
cmd.ExecuteNonQuery() '删除记录
conn.Close()
End Sub