枚举 EnumDescription 位运算 权限 sql c#

简介:
 
 
复制代码
0   1  2  4  8  16, 32, 64, 128, 256, 512, 1024, 20484096, 8192
-- 2的n次方 tinyint类型就够用了
--
0 无权限
--
1 可读(read)
--
2 可新增(insert)
--
4 可修改(update)
--
8 可删除(delete)
--
16 可审核
...
权限的组合
read  + insert  =  1 + 2 = 3 
read  + insert  + delete  =  1 + 2 + 8 = 11
read  +  update + delete  = 1 + 4 + 8 = 13


select  1  |  2     权限的加法 就是逻辑 [ ]运算  -- 结果: 3
select  3  & ( ~ 1)   权限的减法, 使用 [ ]运算 + [ ]运算来实现  -- 结果: 2

select  1  |  13     一次添加n个权限  -- 结果: 13
select  13  & ( ~ 11)   一次减去n个权限  -- 结果:4

select ( 3  &  2)  权限的判断  -- 结果:2

-- 例子
Select  *  From Customer  Where (  @Status  is  null  Or  [ Status ]  &  @Status  =  [ Status ])

int []  power  = new  int [] {  1248163264 };
int value  =  126;       
for ( int i  =  0; i  <  power.Length; i ++)
{
     if ((value  &  power [ i ]!=  0)
    {
        Console.WriteLine("有power [ {0} ] ={ 1}所代表的权限", i,  power [ i ]);
    }
}


-- 读取权限
private  int [] GetPermission( int PermissionSum)
{
    List < int > list  = new List < int >();
     int []  table  = { 1, 2, 4, 8, 16, 32, 64};
     for ( int i  =  table.Length - 1; i  >- 1; i -- )
    {
         if ( table [ i ]  == PermissionSum)
        {
            list. Add( table [ i ]);
             break;
        }
         if ( table [ i ]  > PermissionSum)
        {
             continue;
        }
        PermissionSum  -=  table [ i ];
        list. Add( table [ i ]);
    }
     return list.ToArray();
}

http: //www.cnblogs.com /zhuqil /archive / 2010 / 04 / 02 /Permission.html
复制代码
 
 
 
 
复制代码
using System.ComponentModel; 
public  enum ProjectPhase 

    [Description( " Requirements Gathering ")] 
     RequirementsGathering =  1
    [Description( " Analysis and Design ")] 
     AnalysisDesign =  1
}
// EnumExtensions
public  static  string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fi.GetCustomAttributes(
         typeof(DescriptionAttribute),
         false);

     if (attributes !=  null &&
        attributes.Length >  0)
         return attributes[ 0].Description;
     else
         return value.ToString();
}
复制代码
 
 

Binding an enumeration to a dropdown list

 
 
复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

public  enum ActiveFrom :  int
{
[Description( " Now ")]
DateOfPurchase =  0,
[Description( " Tomorrow ")]
OnLogin =  1,
[Description( " Day After Tomorrow ")]
OnActivation =  2
}

public  static  class Extensions
{
public  static IEnumerable BindWithEnum()  where TEnum :  struct
{
Type enumType =  typeof(TEnum);

Type descriptionAttributeType =  typeof(DescriptionAttribute);

List list =  new List();

foreach ( int value  in Enum.GetValues(enumType))
{
MemberInfo enumObj = enumType.GetMember(Enum.ToObject(enumType, value).ToString()).SingleOrDefault();

object attr = enumObj.GetCustomAttributes(descriptionAttributeType,  false).SingleOrDefault();

if (attr ==  nullcontinue;

string description = ((DescriptionAttribute)attr).Description;

list.Add( new { Code = value, Name = description });
}

return list;
}
}
复制代码
 
 
 
 
复制代码
// Pick a Random Enum in C# (Better than my old post)
static T RandomEnum() {
    if (  typeof( T ).IsEnum ) {
    var names = Enum.GetNames(  typeof( T ) );
    return ( T )Enum.Parse(  typeof( T ), names[ threadsafeRandom.Value.Next(  0, names.Length ) ] );
      }
   return  default( T );
}

static  readonly ThreadLocal threadsafeRandom =  new ThreadLocal( () =>  new Random( RandomSeed() ) );
复制代码
 
 

    本文转自曾祥展博客园博客,原文链接: http://www.cnblogs.com/zengxiangzhan/archive/2010/10/06/1844845.html ,如需转载请自行联系原作者


相关文章
|
SQL 存储 Windows
微软SQL 2019,“无法连接到WMI提供程序,您没有权限或者该服务器无法访问”
微软SQL 2019,解决“无法连接到WMI提供程序,您没有权限或者该服务器无法访问”的方法
155 0
微软SQL 2019,“无法连接到WMI提供程序,您没有权限或者该服务器无法访问”
|
SQL 关系型数据库 MySQL
RH358配置MariaDB SQL数据库--管理MariaDB用户和访问权限
RH358配置MariaDB SQL数据库--管理MariaDB用户和访问权限
317 0
RH358配置MariaDB SQL数据库--管理MariaDB用户和访问权限
|
SQL 关系型数据库 MySQL
MySql8.0.19最新版本创建用户分配权限演示,You have an error in your SQL syntax权限分配问题解决方法
MySql8.0.19最新版本创建用户分配权限演示,You have an error in your SQL syntax权限分配问题解决方法
258 0
|
SQL 监控
PolarDB-X 1.0-用户指南-SQL审计与分析-为子账号授予SQL审计权限
子账号开通或使用PolarDB-X SQL 审计功能之前,需要由主账号为其授权。
120 0
|
SQL 关系型数据库 MySQL
PolarDB-X 1.0-SQL 手册-DAL-账号和权限系统
PolarDB-X 账号和权限系统的用法跟 MySQL 一致,支持 GRANT、REVOKE、SHOW GRANTS、CREATE USER、DROP USER、SET PASSWORD 等语句,目前支持库级和表级权限的授予,全局级别和列级别权限暂时不支持。
304 0
|
SQL Go 数据库
SQL Server遗失管理权限账号密码怎么办?
原文:SQL Server遗失管理权限账号密码怎么办? 假如一个SQL Server实例只允许“SQL身份认证”模式登录数据库,而糟糕的是你忘记了sa的密码(sa出于安全考虑应该被禁用,这里仅仅为了描述问题)或其它具有sysadmin角色的登录名的密码?个人就遇到这样一个案例,HK一同事在一台测试服务器安装了一个测试用途的SQL Server数据库,然后这个同事离职前没有交接这个测试服务器任何信息。
1093 0
|
SQL 存储 Go
SQL Server中授予用户查看对象定义的权限
原文:SQL Server中授予用户查看对象定义的权限 SQL Server中授予用户查看对象定义的权限   在SQL Server中,有时候需要给一些登录名(用户)授予查看所有或部分对象(存储过程、函数、视图、表)的定义权限存。
891 0
|
13天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改
|
23天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0