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();
}
}
查看全文
相关阅读:
MySQL数据库返回影响行数的实际操作流程
nslookup命令
Mysql Strict Mode
mysql表大小写
Objective-C消息转发
NSDateFormatter 和 NSDateComponents 的用法
提交app的时候总是报出icon的错误
IOS 的loadView 及使用loadView中初始化View注意的问题。(死循环并不可怕)
[[NSMutableArray alloc] init];和[[NSMutableArray alloc] initWithCapacity:0]区别
NSMutableArray初始化崩溃问题
原文地址:https://www.cnblogs.com/ahuang1118/p/172566.html
最新文章
属性访问(GP Internal API)
TA接口(GP Internal API)
密码学基础
GP TEE Internal API基础知识
Power BI使用技巧
Oracle常用语句
如何解决PL/SQL Developer过期的情况
部署包含Oracle数据源的项目
SQL 清除数据库中所有表的数据
全库修改SQL Server现有排序规则
热门文章
SSIS连接Oracle问题汇总
PLSQL安装配置与汉化
PBI自定义视觉对象环境配置
Power BI DAX 公式使用
mysql 模糊查询
mysql insert语句后如何获取insert数据的主键值自动编号
mysql基础笔记
MySQL 触发器简单实例
centos 安装mysql
linux virtual box 增强功能 挂载共享文件夹
Copyright © 2011-2022 走看看