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();
}
}
查看全文
相关阅读:
iOS 关于NSNotificationCenter
IOS UITableView reload 刷新某一个cell 或 section
IOS AFNetWorking 设置超时时间
IOS AFNetWorking
IOS 十六进制字符串转换成UIColor
IOS 长姿势---双击Home键
IOS 关于 NSUserDefault
方正璞华培训讲师
localStorage使用总结
Promise的简单用法
原文地址:https://www.cnblogs.com/ahuang1118/p/172566.html
最新文章
popwindow--弹窗
bootstrip 弹窗
购物车
给一个文件夹,删除该文件夹
给一个文件夹,返回该文件夹下所有文件的数量
文件上传 ————图片
AJAX-点击显示 出现表单内容
AJAX-即使出现 有关写入的 搜查结果
失去焦点 显示是否用户名存在
Python 练习题
热门文章
Python 第三十六章 生产者模式+递归锁+信号量+GIL全局+IO、计算密集型验证+线程池和进程池
Python 第三十五章 线程 构建线程+线程开启速度+线程pid+数据共享+join+互斥锁+守护线程+其他方法
Python 第三十四章 进程通信 文件通信+队列通信 + 锁 join+ lock
Python 第三十三章 进程进阶 进程创建+空间隔离+其他参数+守护进程+僵尸进程和孤儿进程
Python 第三十三章 进程初识
Python 第三十二章 操作系统基础
博客园的样式
Python 第三十一章 UDP协议的socket
Python 第三十章 粘包现象
iOS define 宏定义 和 const定义常量区别
Copyright © 2011-2022 走看看