C# Web控件与数据感应之 CheckBoxList 类

server/2024/9/22 15:38:36/

目录

关于数据感应

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)用于列出可用的角色,其结构如下:

序号字段名类型说明备注
1ciduniqueidentifier唯一ID用于后续方法使用
2charnamenvarchar(30)角色名称

其数据示例如下:

用户角色表

表(sys_UserChars)用于存储用户的可用角色(用户ID+角色ID 唯一),其结构如下:

序号字段名类型说明备注
1user_ciduniqueidentifier用户ID用户的ID值
2char_ciduniqueidentifier角色名称用记所属的角色ID值

其示例数据如下:

AutoValueDBList 方法

原理

我们需要提取 sys_chars (角色字典表) 数据绑定到 CheckBoxList 控件上,用于显示可用的角色名称。绑定后通过 AutoValueDBList 方法的查询模式,从 sys_UserChars (用户角色表)提取数据并与 CheckBoxList 上的项进行比对,存在的则选中。同理,使用 AutoValueDBList 方法的保存模式,则将用户在 CheckBoxList 上的选项逐一保存到 sys_UserChars (用户角色表)里。

设计

AutoValueDBList 方法主要分查询模式和保存模式,在保存模式的情况下返回成功影响的行数,其参数说明如下表:

序号参数名类型说明
1strConnstring对应数据库的连接字符串
2_objectListControl要感应的 ListControl 类控件,这里泛指 CheckBoxList
3AutoTypestring

两种值可选择,“query” 为查询模式,“save” 为保存模式

4keyFieldTypestring

连接的目标表的关键字字段类型,如 uniqueidentifier,比如sys_UserChars 中的 user_cid 字段类型

5linkKeyValuestring连接的目标表的关键字段的值,比如sys_UserChars 中的 user_cid 字段的值
6Tablenamestring要连接的目标表比如 sys_UserChars
7KeyFieldstring连接的目标表的关键字字段名,比如sys_UserChars 中的字段 “user_cid” 
8KeyField2string连接的目标表的第二关键字字段名,比如sys_UserChars 中的字段 “char_cid” 
9CidFieldNamestring指定连接目标表的唯一标识字段名,这里仅允许使用 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<_object.Items.Count;i++){string _value=_object.Items[i].Value;if(!_object.Items[i].Selected){continue;}para2.Value=(keyFieldType.ToLower()=="uniqueidentifier"?(object)(new Guid(_value)):(object)_value);int si=Cmd.ExecuteNonQuery();success+=si;}return success;}catch (Exception ex){return rv;}finally{Conn.Close();Conn.Dispose();}}if(AutoType=="query"){SqlDataReader myDr;Cmd.CommandText = " select "+KeyField2+" 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();myDr=Cmd.ExecuteReader();for(int i=0;i<_object.Items.Count;i++){_object.Items[i].Selected=false;}rv=0;while(myDr.Read()){rv++;string _dbkey=myDr[KeyField2].ToString();for(int i=0;i<_object.Items.Count;i++){string _value=_object.Items[i].Value;if(_value==_dbkey){_object.Items[i].Selected=true;}}}}catch (Exception ex){return rv;}finally{Conn.Close();Conn.Dispose();}}return rv;}public SqlDbType GetSqlDbType(String TypeInfo) 
{TypeInfo=TypeInfo.ToLower();//Byteif((TypeInfo=="varchar")||(TypeInfo=="system.string")){return SqlDbType.VarChar;}if((TypeInfo=="bigint")||(TypeInfo=="system.int64")){return SqlDbType.BigInt;}if((TypeInfo=="binary")||(TypeInfo=="system.byte[]")){return SqlDbType.Binary;}if((TypeInfo=="bit")||(TypeInfo=="system.boolean")){return SqlDbType.Bit;}if((TypeInfo=="char")||(TypeInfo=="system.char")){return SqlDbType.Char;}if((TypeInfo=="datetime")||(TypeInfo=="system.datetime")){return SqlDbType.DateTime;}if((TypeInfo=="decimal")||(TypeInfo=="system.decimal")){return SqlDbType.Decimal;}if((TypeInfo=="float")||(TypeInfo=="system.double")){return SqlDbType.Float;}if(TypeInfo=="image"){return SqlDbType.Image;}if((TypeInfo=="int")||(TypeInfo=="system.int32")){return SqlDbType.Int;}if((TypeInfo=="money")||(TypeInfo=="system.decimal")){return SqlDbType.Money;}if(TypeInfo=="nchar"){return SqlDbType.NChar;}if(TypeInfo=="ntext"){return SqlDbType.NText;}if(TypeInfo=="nvarchar"){return SqlDbType.NVarChar;}if((TypeInfo=="real")||(TypeInfo=="system.single")){return SqlDbType.Real;}if(TypeInfo=="smalldatetime"){return SqlDbType.SmallDateTime;}if((TypeInfo=="smallint")||(TypeInfo=="system.int16")){return SqlDbType.SmallInt;}if(TypeInfo=="smallmoney"){return SqlDbType.SmallMoney;}if(TypeInfo=="text"){return SqlDbType.Text;}if((TypeInfo=="timestamp")||(TypeInfo=="system.timespan")){return SqlDbType.Timestamp;}if((TypeInfo=="tinyint")||(TypeInfo=="system.byte")){return SqlDbType.TinyInt;}if((TypeInfo=="uniqueidentifier")||(TypeInfo=="system.guid")){return SqlDbType.UniqueIdentifier;}if(TypeInfo=="varbinary"){return SqlDbType.VarBinary;}if(TypeInfo=="variant"){return SqlDbType.Variant;}return SqlDbType.VarChar;
}// end GetSqlDbType function 	

调用示例

初始化数据

假设前端 UI 有 ID  为 CBL 的 CheckBoxList 控件,则调用的示例代码如下: 

simpleDataListEx("sqlserver","数据库连接串","select cid, charname from sys_chars", null, "cid", "charname", CBL, false, "", "","");

有关 simpeDataListEx 的使用方法请阅读我的文章《C# Web控件与数据感应之 Control 类》。 

启动查询模式

初始化完成后,启动 AutoValueDBList 方法的查询模式,进行比对操作。示例代码如下:

string user_cid_value="1F044A84-9154-466B-9B9F-894282625729";
AutoValueDBList("你的数据库连接串",CBL, "query", "uniqueidentifier", user_cid_value, "sys_userchars", "user_cid", "char_cid");

使用保存模式

当用户重新进行复选操作时,可以将结果提交给数据库进行保存。示例代码如下:

string user_cid_value="1F044A84-9154-466B-9B9F-894282625729";
AutoValueDBList("你的数据库连接串",CBL,"save","uniqueidentifier",user_cid_value,"sys_userchars","user_cid","char_cid","cid");

小结

范例中使用的MS SQL SERVER 数据库,我的下载资源还提供了 Oracle 9i及达梦数据库的驱动链接库,请下载我的资源:

https://download.csdn.net/download/michaelline/89235824

我们可以根据需要改造方法,另外 AutoValueDBList 方法基于 ListControl 类,我们可以根据实际的需要进行使用和改造。感谢您的阅读,希望本文能够对您有所帮助。


http://www.ppmy.cn/server/25370.html

相关文章

NL2SQL技术方案系列(5):金融领域NL2SQL技术方案以及行业案例实战讲解3--非LLM技术方案

NL2SQL技术方案系列(5):金融领域NL2SQL技术方案以及行业案例实战讲解3 NL2SQL基础系列(1):业界顶尖排行榜、权威测评数据集及LLM大模型(Spider vs BIRD)全面对比优劣分析[Text2SQL、Text2DSL] NL2SQL基础系列(2):主流大模型与微调方法精选集,Text2SQL经典算法技术回顾七…

Python数据分析系列(五):python数据结构 — Pandas中的Series使用

文章目录 前言一、Series创建与属性二、Series的索引三、Series的基本运算四、Series的数据对齐五、Series操作1、判断是否是唯一值2、判断值3、值计数4、缺失值处理1、滤除缺失数据2、填充缺失数据5、日期时间列中提取月份和年份前言 Pandas 是基于 NumPy 的一种工具,该工具…

商城数据库88章表80~83

schooldb库——utf8字符集——utf8_general_ci排序规则 先创建库&#xff0c;再去使用下列的DDL语句。 &#xff08;80&#xff09;DDL——商城职员表 CREATE TABLE huang_staffs (staffId int(11) NOT NULL AUTO_INCREMENT COMMENT 自增ID,loginName varchar(40) NOT NULL …

IOPS ;MB/S分别在衡量RAN/SEQ R/W的原因说明

IOPS&#xff08;Input/Output Operations Per Second&#xff09; 定义&#xff1a;IOPS是指存储设备每秒可以完成的读写操作的次数。它主要用来衡量随机读写性能。 适用场景&#xff1a;IOPS常用于衡量在大量小文件传输或高并发数据库操作等场景下的性能。在这些情况下&…

费曼学习法个人总结-1

2024年4月27日 1&#xff0c;知道它叫什么和懂得它是什么是两件事&#xff0c;要了解事物的本质&#xff0c;不应该只是了解名字。 2&#xff0c;输出倒逼输入。 3&#xff0c;实施的5步骤 调整心态&#xff0c;开始行动指定目标&#xff0c;养成习惯找对方法&#xff0c;高…

新科技辅助器具赋能视障生活:让盲人出行融入日常

随着科技日新月异的发展&#xff0c;一款名为蝙蝠避障专为改善盲人日常生活的盲人日常生活辅助器具应运而生&#xff0c;它通过巧妙整合实时避障与拍照识别功能&#xff0c;成功改变了盲人朋友们的生活格局&#xff0c;为他们提供了更为便捷、高效的生活体验。 这款非同…

火车头采集怎么发布到Wordpress

火车头采集怎么快速发布到Wordpress系统&#xff0c;可以按照以下步骤操作&#xff1a; 目录 1. Wordpress火车头采集发布模块 2. 发布模块内容参数映射&#xff0c;火车头采集发布数据到Wordpress 3. 简数采集一键发布到Wordpress方法 1. Wordpress火车头采集发布模块 如…

XTuner微调LLM:1.8B、多模态和Agent-笔记四

本次课程由XTuner 贡献者李剑锋、汪周谦、王群老师讲解【XTuner 微调 LLM&#xff1a;1.8B、多模态和 Agent】课程 课程视频&#xff1a;http:// https://b23.tv/QUhT6ni 课程文档&#xff1a;https://github.com/InternLM/Tutorial/blob/camp2/xtuner/readme.md 两种Finetun…