Global00.asax.cs
上传用户:tree100901
上传日期:2007-06-03
资源大小:2295k
文件大小:5k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Net;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Web;
  6. using System.IO;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Web.SessionState;
  10. namespace insideweb 
  11. {
  12. /// <summary>
  13. /// Global 的摘要说明。
  14. /// </summary>
  15. public class Global : System.Web.HttpApplication
  16. {
  17. /// <summary>
  18. /// 必需的设计器变量。
  19. /// </summary>
  20. private System.ComponentModel.IContainer components = null;
  21. public Global()
  22. {
  23. InitializeComponent();
  24. }
  25. protected void Application_Start(Object sender, EventArgs e)
  26. {
  27. Application["user_sessions"] = 0;
  28. }
  29.  
  30. protected void Session_Start(Object sender, EventArgs e)
  31. {
  32. Int32 count;
  33. string UserHostAddress;
  34. string UserHostName;
  35. SqlConnection MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  36. if (Session["whenLogin"]==null)
  37. {
  38. Response.Redirect("index.htm");
  39. Session["whenLogin"]="now";
  40. }
  41. if(Session["online"]==null)
  42. {
  43. Application.Lock();
  44. Application["user_sessions"] = (int)Application["user_sessions"] + 1;
  45. Application.UnLock();
  46. UserHostAddress=Request.UserHostAddress ;
  47. IPHostEntry myHost = Dns.GetHostByAddress(Request.UserHostAddress);
  48. UserHostName=myHost.HostName;
  49. try
  50. {   
  51. Application["UserHostName"]=myHost.HostName;
  52. Application["IP"]=UserHostAddress;
  53. SqlCommand MyCommand0 = new SqlCommand("SELECT  * FROM OnlineUser WHERE IP=@IP",MyConnection);
  54. MyCommand0.CommandType = CommandType.StoredProcedure;
  55. MyCommand0.Parameters.Add(new SqlParameter("@IP",SqlDbType.NVarChar, 50));
  56. MyCommand0.Parameters["@IP"].Value = Request.UserHostAddress;
  57. if(MyConnection.State.ToString()=="Closed")
  58. MyConnection.Open();  
  59. count = (Int32)MyCommand0.ExecuteScalar();
  60. MyConnection.Close();  
  61. }
  62. finally
  63. {
  64. }
  65. if(count <0)
  66. {
  67. try
  68. {
  69. MyConnection.Open();  
  70. SqlCommand MyCommand=new SqlCommand("Insert INTO OnlineUser(IP,UserHostName) values (@IP,@UserHostName)",MyConnection);
  71. MyCommand.CommandType = CommandType.StoredProcedure;
  72. MyCommand.Parameters.Add(new SqlParameter("@IP",SqlDbType.NVarChar, 50));
  73. MyCommand.Parameters["@IP"].Value = UserHostAddress;
  74. MyCommand.Parameters.Add(new SqlParameter("@UserHostName", SqlDbType.NVarChar, 50));
  75. MyCommand.Parameters["@UserHostName"].Value = UserHostName;   
  76. MyCommand.ExecuteNonQuery();
  77. MyConnection.Close();
  78. }
  79. catch
  80. {
  81. }
  82. }
  83. else
  84. {
  85. try
  86. {
  87. DateTime now1= DateTime.Now;
  88. MyConnection.Open();  
  89. SqlCommand MyCommand1=new SqlCommand("Update OnlineUser Set UserHostName=@UserHostName WHERE IP=@IP",MyConnection);
  90. MyCommand1.CommandType = CommandType.StoredProcedure;
  91. MyCommand1.Parameters.Add(new SqlParameter("@IP",SqlDbType.NVarChar, 50));
  92. MyCommand1.Parameters["@IP"].Value = UserHostAddress;
  93. MyCommand.Parameters.Add(new SqlParameter("@UserHostName", SqlDbType.NVarChar, 50));
  94. MyCommand.Parameters["@UserHostName"].Value = UserHostName;   
  95. MyCommand1.ExecuteNonQuery();
  96. MyConnection.Close();
  97. }
  98. catch
  99. {
  100. }
  101. }
  102. Session["online"]="set";
  103. }
  104. }
  105. protected void Application_BeginRequest(Object sender, EventArgs e)
  106. {
  107. }
  108. protected void Application_EndRequest(Object sender, EventArgs e)
  109. {
  110. }
  111. protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  112. {
  113. }
  114. protected void Application_Error(Object sender, EventArgs e)
  115. {
  116. }
  117. protected void Session_End(Object sender, EventArgs e)
  118. {
  119. Application.Lock();
  120. Application["user_sessions"] = (int)Application["user_sessions"] - 1;
  121. Application.UnLock();
  122. SqlConnection MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
  123. SqlCommand MyCommand2=new SqlCommand("Delete from OnlineUser where IP=@IP",MyConnection);
  124. MyCommand2.Parameters.Add(new SqlParameter("@IP", SqlDbType.NVarChar, 50));
  125. MyCommand2.Parameters["@IP"].Value = Application["IP"];
  126. MyCommand2.Connection.Open();
  127. try
  128. {
  129. MyCommand2.ExecuteNonQuery();
  130. }
  131. catch (SqlException)
  132. {
  133. }
  134. MyCommand2.Connection.Close();
  135. }
  136. protected void Application_End(Object sender, EventArgs e)
  137. {
  138. }
  139. #region Web 窗体设计器生成的代码
  140. /// <summary>
  141. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  142. /// 此方法的内容。
  143. /// </summary>
  144. private void InitializeComponent()
  145. {    
  146. this.components = new System.ComponentModel.Container();
  147. }
  148. #endregion
  149. }
  150. }