目录
关于数据感应
CheckBoxList 类
范例运行环境
数据源表设计
角色字典表
用户角色表
AutoValueDBList 方法
原理
设计
实现
调用示例
初始化数据
启动查询模式
使用保存模式
小结
关于数据感应
数据感应也即数据捆绑,是一种动态的,Web控件与数据源之间的交互,本文将继续介绍与数据库提取数据并捆绑到 CheckBoxList 类控件为例,另外同时将控件的值保存回数据库的通用方法。
CheckBoxList 类
System.Web.UI.WebControls.CheckBoxList 类是提供了一组可复选的选项集合,每个选项以true或false 表示其选中状态。其使用方法基于 ListControl 类。
更多 CheckBoxList 类的介绍请参照如下链接:
https://learn.microsoft.com/zh-cn/previous-versions/visualstudio/design-tools/expression-studio-2/cc294907(v=expression.10)
范例运行环境
操作系统: Windows Server 2019 DataCenter
.net版本: .netFramework4.7.1 或以上
开发工具:VS2019 C#
数据提取:在这里我们以MS SQL Server 2016为例
数据源表设计
我们假设要为用户添加角色权限,则需要涉及两个表:
角色字典表
表(sys_chars)用于列出可用的角色,其结构如下:
序号 | 字段名 | 类型 | 说明 | 备注 |
---|---|---|---|---|
1 | cid | uniqueidentifier | 唯一ID | 用于后续方法使用 |
2 | charname | nvarchar(30) | 角色名称 |
其数据示例如下:
用户角色表
表(sys_UserChars)用于存储用户的可用角色(用户ID+角色ID 唯一),其结构如下:
序号 | 字段名 | 类型 | 说明 | 备注 |
---|---|---|---|---|
1 | user_cid | uniqueidentifier | 用户ID | 用户的ID值 |
2 | char_cid | uniqueidentifier | 角色名称 | 用记所属的角色ID值 |
其示例数据如下:
AutoValueDBList 方法
原理
我们需要提取 sys_chars (角色字典表) 数据绑定到 CheckBoxList 控件上,用于显示可用的角色名称。绑定后通过 AutoValueDBList 方法的查询模式,从 sys_UserChars (用户角色表)提取数据并与 CheckBoxList 上的项进行比对,存在的则选中。同理,使用 AutoValueDBList 方法的保存模式,则将用户在 CheckBoxList 上的选项逐一保存到 sys_UserChars (用户角色表)里。
设计
AutoValueDBList 方法主要分查询模式和保存模式,在保存模式的情况下返回成功影响的行数,其参数说明如下表:
序号 | 参数名 | 类型 | 说明 |
---|---|---|---|
1 | strConn | string | 对应数据库的连接字符串 |
2 | _object | ListControl | 要感应的 ListControl 类控件,这里泛指 CheckBoxList |
3 | AutoType | string | 两种值可选择,“query” 为查询模式,“save” 为保存模式 |
4 | keyFieldType | string | 连接的目标表的关键字字段类型,如 uniqueidentifier,比如sys_UserChars 中的 user_cid 字段类型 |
5 | linkKeyValue | string | 连接的目标表的关键字段的值,比如sys_UserChars 中的 user_cid 字段的值 |
6 | Tablename | string | 要连接的目标表比如 sys_UserChars |
7 | KeyField | string | 连接的目标表的关键字字段名,比如sys_UserChars 中的字段 “user_cid” |
8 | KeyField2 | string | 连接的目标表的第二关键字字段名,比如sys_UserChars 中的字段 “char_cid” |
9 | CidFieldName | string | 指定连接目标表的唯一标识字段名,这里仅允许使用 uniqueidentifier 的类型字段,如无则默认不参于 insert 操作,设置则表示其值为 newid() |
实现
AutoValueDBList 方法完整代码如下:
public int AutoValueDBList(string strConn,ListControl _object,string AutoType,string keyFieldType,string linkKeyValue,string Tablename,string KeyField,string KeyField2,string CidFieldName) { int rv=-1; SqlDbType type=GetSqlDbType(keyFieldType); SqlConnection Conn = new SqlConnection(strConn ); SqlCommand Cmd = new SqlCommand(); Cmd.Connection = Conn; if(AutoType=="save") { Cmd.CommandText = " delete from "+Tablename+" where "+KeyField+"=@"+KeyField; SqlParameter para1=new SqlParameter("@"+KeyField,type); para1.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(linkKeyValue)):(object)linkKeyValue); Cmd.Parameters.Add(para1); try { Conn.Open(); Cmd.ExecuteNonQuery(); Cmd.CommandText = " insert into "+Tablename+"("+KeyField+","+KeyField2+") values(@"+KeyField+",@"+KeyField2+")"; if(CidFieldName!="") { Cmd.CommandText = " insert into "+Tablename+"("+CidFieldName+","+KeyField+","+KeyField2+") values(newid(),@"+KeyField+",@"+KeyField2+")"; } SqlParameter para2=new SqlParameter("@"+KeyField2,type); Cmd.Parameters.Add(para2); int success=0; for(int i=0;i