zoukankan
html css js c++ java
如何实现计数器
在
Global.asax.cs
文件中的分别输入以下代码:
protected
void
Application_Start(Object sender, EventArgs e)
{
SqlConnection con;
SqlCommand cmd;
//
Get the connection string from the existing key in Web.config
con
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
cnFriends.ConnectionString
"
]);
cmd
=
new
SqlCommand(
"
SELECT Visitors FROM Counter
"
, con);
con.Open();
protected
void
Session_Start(Object sender, EventArgs e)
{
Application.Lock();
Application[
"
counter
"
]
=
((
int
)Application[
"
counter
"
])
+
1
;
Application.UnLock();
}
try
{
//
Retrieve the counter
Application[
"
counter
"
]
=
(
int
) cmd.ExecuteScalar();
}
finally
{
con.Close();
}
}
protected
void
Application_AuthenticateRequest(Object sender, EventArgs e)
{
//
Cast the sender to the application
HttpApplication app
=
(HttpApplication)sender;
//
Only replace the context if it has already been handled
//
by forms authentication module (user is authenticated)
if
(app.Request.IsAuthenticated)
{
SqlConnection con;
string
sql;
SqlCommand cmd;
string
id
=
Context.User.Identity.Name;
con
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
cnFriends.ConnectionString
"
]);
sql
=
"
SELECT IsAdministrator FROM [User] WHERE UserId='{0}'
"
;
sql
=
String.Format(sql, id);
cmd
=
new
SqlCommand(sql, con);
con.Open();
//
Ensure closing the connection
try
{
object
admin
=
cmd.ExecuteScalar();
//
Was it a valid UserID?
if
(admin
!=
null
)
{
GenericPrincipal ppal;
string
[] roles;
//
If IsAdministrator field is true, add both roles
if
(((
bool
)admin)
==
true
)
{
roles
=
new
string
[]
{
"
User
"
,
"
Admin
"
}
;
}
else
{
roles
=
new
string
[]
{
"
User
"
}
;
}
ppal
=
new
GenericPrincipal(Context.User.Identity, roles);
Context.User
=
ppal;
}
else
{
//
If UserID was invalid, clear the context so he logs on again
Context.User
=
null
;
}
}
catch
{
throw
;
}
finally
{
con.Close();
}
}
}
protected
void
Application_End(Object sender, EventArgs e)
{
SqlConnection con;
SqlCommand cmd;
//
Get the connection string from the existing key in Web.config
con
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
cnFriends.ConnectionString
"
]);
cmd
=
new
SqlCommand(
"
UPDATE Counter SET Visitors=
"
+
Application[
"
counter
"
].ToString(), con);
con.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
查看全文
相关阅读:
整数转字符串
SharePoint介绍性文章
Disable Sharepoint 2007 show as System Account when system admin login
通过IP地址获得主机名
从文本文件读取信息
数据库连接池问题[转]
企业类库问题 public key 问题[经过自己测试]
Google Analytics异步代码创建虚拟浏览量跟踪
同一主机上WordPress博客更换域名简易八步骤(2)
关于application/xwwwformurlencoded等字符编码的解释说明
原文地址:https://www.cnblogs.com/ahuang1118/p/172566.html
最新文章
—(二)水晶报表(CrystalReports)的简单应用(配置及发布)
利用Myeclipse快速开发struts应用程序
aspnetpager的使用(完美篇)
用MyEclipse创建一个Struts+Hibernate项目
aspnetpager的使用
JSP页面中使用FCKeditor控件(js用法)
Firebug入门指南
匹配ip段、ip转换为long型、屏蔽ip段(InetAddress类)
ie和firefox不兼容的解决方法集合
Java创建cookie和删除cookie
热门文章
Firebug安装与使用详解
div+css学习笔记(IE与fox好多不兼容的问题)
最实用的js表单验证方法
js checkbox(复选框) 使用集锦
Listener Servlet的应用
中英文对照术语表
Silverlight插件的位置和尺寸(Silverlight 文档)
链接:一篇介绍ASP.NET 2.0 Membership的文章
统计字符出现的次数
考虑无符号整数的二进制中1的个数
Copyright © 2011-2022 走看看