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();
}
}
查看全文
相关阅读:
EVRYTHNG.H
关于轮胎尺寸问题
常见内核数据结构.doc
i5处理器的台式机[百度知道]
debug和release版区别
booklist 转
windows 系统编程 Chap7 线程和调度
一个超级简单的dwr配置文件,介绍了dwr最常用的几个标签(转)
用凭据管理器提升Windows7访问速度(非原创)
IEC87005104 传输规约(国电)
原文地址:https://www.cnblogs.com/ahuang1118/p/172566.html
最新文章
N46期第二周作业
N46期第六周作业
让FileDisassembler支持VS 2008
用Reflector和FileDisassembler配合反编译.net Windows程序
如何截取小数点后几位,(C floor函数例子)
Downgrade a VS 2008 .sln or .csproj to VS 2005(转)
QQ连连看辅助程序 (附程序下载)
弄个DotNetBar来用
反编译飞信(修改,支持多开)
基于eclipse 的再开发
热门文章
linux系统增加消息队列长度
使用CSS3完成阴阳师宣传页
js实现数组去重
课程作业01
课上动手动脑
《大道至简》第二章读后感
《大道至简》第一章读后感
Windows系统编程chap6
Windows系统编程 chap5
windows code
Copyright © 2011-2022 走看看