创建和配置ASP.NET Session状态数据库
在基于NLB(网络负载平衡)环境下的ASP.NET Web应用程序开发,我们需要将Session存储在数据库中供多个Web应用程序调用,以下为配置方法及注意事项。
1.创建用于存储ASP.NET Session的数据库(远程、本地皆可,使用数据库用户身份认证) 在Windows\Microsoft.NET\Framework/V2.0.50727目录下使用如下命令: aspnet_regsql.exe -S <SQL Server IP> -U <User Name> -P <Password> -E -ssadd -sstype c -d <Database Name> 命令执行后就会成功建立起用于存储ASP.NET Session变量的数据库了。
2.Web.Config文件配置项 我们需要在ASP.NET Web应用程序中的Web.Config文件修改sessionState配置项以使Session状态数据库生效。 配置节点如下: <sessionState mode="SQLServer"             sqlConnectionString="server=<Server IP>;database=<Database Name>;uid=<User Name>;pwd=<Password>;" allowCustomSqlDatabase="True"             cookieless="false"             timeout="20" />
3.注意在进行系统测试(主要是负载测试)的时候,因为数据库访问负载的增加,需要调整SQL Server相应超时的配置项以适应负载。(默认值为10,请适度进行调整。)
ASP.NET Session状态数据库数据模型 1.ASPStateTempSessions表定义

列名 类型 描述
SessionId nvarchar(88) Session ID + application ID
Created datetime Date and time session was created (UTC)
Expires datetime Date and time session expires (UTC)
LockDate datetime UTC date and time session was locked
LockDateLocal datetime Local date and time session was locked
LockCookie int Lock ID
Timeout int Session timeout in minutes
Locked bit 1=Session locked, 0=Session not locked
SessionItemShort varbinary(7000) Serialized session state (if <= 7,000 bytes)
SessionItemLong image Serialized session state (if > 7,000 bytes)
Flags int Session state flags (1=Uninitialized session)

2.ASPStateTempApplications表定义

列名 类型 描述
AppId int Application ID
AppName char(280) Application name

3.使用的存储过程

Stored Procedure Description
CreateTempTables Creates the ASPStateTempSessions and ASPStateTempApplications tables; called during setup, but not called by SqlSessionStateStore.
DeleteExpiredSessions Used by SQL Server Agent to remove expired sessions.
GetHashCode Hashes an application name and returns the hash; called by TempGetAppID.
GetMajorVersion Returns SQL Server's major version number.
TempGetAppID Converts an application name into an application ID; queries the ASPStateTempApplications table and inserts a new record if necessary.
TempGetStateItem Retrieves read-only session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).
TempGetStateItem2 Retrieves read-only session state from the database (ASP.NET 1.1).
TempGetStateItem3 Retrieves read-only session state from the database (ASP.NET 2.0).
TempGetStateItemExclusive Retrieves read/write session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7).
TempGetStateItemExclusive2 Retrieves read/write session state from the database (ASP.NET 1.1).
TempGetStateItemExclusive3 Retrieves read/write session state from the database (ASP.NET 2.0).
TempGetVersion Marker whose presence indicates to ASP.NET 2.0 that the session state database is ASP.NET 2.0-compatible.
TempInsertStateItemLong Adds a new session, whose size is > 7,000 bytes, to the database.
TempInsertStateItemShort Adds a new session, whose size is <= 7,000 bytes, to the database.
TempInsertUninitializedItem Adds a new uninitialized session to the database in support of cookieless sessions.
TempReleaseStateItemExclusive Releases a lock on a session; called when ASP.NET determines that a request has timed out and calls the provider's ReleaseItemExclusive method.
TempRemoveStateItem Removes a session from the database when the session is abandoned.
TempResetTimeout Resets a session's timeout by writing the current date and time to the corresponding record's Expires field.
TempUpdateStateItemLong Updates a session whose size is > 7,000 bytes.
TempUpdateStateItemLongNullShort Updates a session whose old size is <= 7,000 bytes, but whose new size is > 7,000 bytes.
TempUpdateStateItemShort Updates a session whose size is <= 7,000 bytes.
TempUpdateStateItemShortNullLong Updates a session whose old size is > 7,000 bytes, but whose new size is <= 7,000 bytes.

ASP.NET 状态数据库FAQ
1.如果把SESSION值存放到数据库中去,用户关闭了程序那怎么样清空数据库里的SESSION值呢?    实际ASP.NET在创建状态数据库的时候会在SQL Server代理(SQL Server Agent)的作业中添加一个作业,名称为<状态数据库名>_Job_DeleteExpiredSessions。如果打开SQL Server代理服务数据库可以通过添加的状态记录的超时时间字段(Exprires)定期对超时的状态数据进行删除。
2.ASPStateTempSessions表中的SessionId字段如何使用? 数据库中此表的SessionID字段的值,由SessionID和AppID共同组成,最后8位为AppID所以,后8位之前一定是SessionID。例如,存储在数据库中的值为"ekr30c3mwvnc3145yrswew3a037e5e5a",后8位的"037e5e5a"为AppID,而前面的"ekr30c3mwvnc3145yrswew3a"为应用程序中你可以使用Session.SessionID获得的字符串。
3.如何判断Session何时被更新的? Session记录被更新时会同时更新Expires和LockDateLocal,Expires字段为UTC时间,如果想通过本地之间进行比较判断还是需要使用LockDateLocal。
4.获得Web.config配置文件节点信息的程序?

Code ''获得Web.config文件配置实例Dim configuration As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/web.config")
''获得状态配置节点实例Dim mSessionStateSection As System.Web.Configuration.SessionStateSection =CType(configuration.GetSection("system.web/sessionState"),System.Web.Configuration.SessionStateSection)
''获得状态模式Response.Write(mSessionStateSection.Mode) ''获得状态超时时间Response.Write(mSessionStateSection.Timeout)

转载于:https://www.cnblogs.com/zxktxj/archive/2012/03/09/2387416.html

使用SQL Server存储ASP.NET Session变量相关推荐

  1. 采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)

    前言 之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看. ASP.NET Core之跨平台的实时性能监控 ASP.NET Core之跨平台的实时性能监控(2.健康检查) ...

  2. SQL Server存储机制

    SQL Server存储机制 1.区段 区段(extent)是用来为表和索引分配空间的基本存储单元.它由8个连续的64KB数据页组成. 基于区段(而不是实际使用空间)分配空间的概念的要点: 一旦区段已 ...

  3. 实现基于SQL Server存储账号的MDaemon群集

    本章概述      MDaemon Server(以下简称 MDaemon) 的冗余方案有多种选择,本章将以 微软 公司出品的 SQL Server 2005 简体中文企业版(以下简称 SQL2005 ...

  4. SQL Server中临时表与表变量的区别

    我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临时表及表变量.在实际使用的时候,我们如何灵活的在存储过程中运用它们,虽然它们实现的功能基本上是一样的,可如何在一个存储过程中有时候 ...

  5. sql server 2008学习8 sql server存储和索引结构

    sql server的存储机制 区段: 是用来为表和索引 分配空间的基本存储单元. 由 8个连续的页面构成,大小为64kb. 区段的注意事项: 一旦区段已满,那么下一记录 将要占据的空间不是记录的大小 ...

  6. SQL Server--[转]SQL Server中临时表与表变量的区别

    http://blog.csdn.net/skyremember/archive/2009/03/05/3960687.aspx 我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临 ...

  7. 【转】如何实现小型WEB搜索引擎(C# SQL Server全文检索 Asp.net)

    SOSO-----为您量身定做的 WEB搜索引擎 1 引言 21世纪,中国互联网搜索引擎领域可谓群雄逐鹿,百度.Yahoo.中搜.搜狗等等都使出浑身解数吸引着网民的眼球.这些大网站可谓是各有所长,总的 ...

  8. SQL Server 存储引擎-剖析Forwarded Records

    我们都知道数据在存储引擎中是以页的形式组织的,但数据页在不同的组织形式中其中对应的数据行存储是不尽相同的,这里通过实例为大家介绍下堆表的中特有的一种情形Forwared Records及处理方式. 概 ...

  9. SQL SERVER 存储大全以及常见实例

    xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 xp_loginconfig --*报告SQL Server 实例在 ...

最新文章

  1. 相关性分析p值_一行代码掌握皮尔逊相关分析,洞察变量关系
  2. 如何手工删除AD RMS SCP?
  3. Select的OnChange()事件中获取选中的值
  4. 滑动拼图验证码操作步骤:_拼图项目:一个不完整的难题
  5. 轉:VB6中将数据导出到Excel提速之法
  6. 以终端模式连接远程桌面
  7. python字典操作首字母与星期的对应_python小课堂10 - 基本数据类型终篇集合和字典...
  8. 2016.6.2近日学习计划
  9. 图书管理系统完整代码
  10. C语言简单通讯录模板
  11. 谷粒商城-个人笔记(基础篇一)
  12. matlab实现adf检验,ADF检验MATLAB程序资料
  13. ctf编码,解密总结
  14. SPOOLing和虚拟化
  15. 生兔子问题(递归算法)
  16. 奇技淫巧玄妙无穷| M1 mac os(苹果/AppleSilicon)系统的基本操作和设置
  17. 奔驰采用鸿蒙系统,余承东官宣:鸿蒙系统正式登陆奔驰S级
  18. 电路+模电+电力电子基础
  19. .json格式是什么?如何快速打开.json文件?
  20. 微信h5支付 ajax,H5支付跳转问题

热门文章

  1. LeetCode 1055. 形成字符串的最短路径(贪心)
  2. LeetCode 1236. 网络爬虫(BFS/DFS)
  3. LeetCode 1056. 易混淆数(哈希)
  4. 潜在语义分析(Latent Semantic Analysis,LSA)
  5. java 判断请求为 ajax请求_Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,java 判断请求是不是ajax请求...
  6. 最炫国漫《雾山五行》用 Python 了解一下到底有多优秀
  7. ClickHouse表引擎之Integration系列
  8. linux nginx 图片服务器,搭建Nginx图片服务器(Linux)
  9. firewall mysql端口_Centos7 firewall开放3306端口
  10. deepfashion 深度学习_基于Alluxio加速混合云下的Intel Analytics Zoo开源深度学习平台...