如何在数据库中插入复选框值 [英] How to Insert checkbox values in database

查看:71
本文介绍了如何在数据库中插入复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格视图。带有复选框的模板字段用于添加,查看,编辑。如果我检查文本框,那么在数据库y值应该插入否则N.我的网格视图是这样的



Role_Id Role_Name添加视图编辑删除

1管理员chk1 chk2 chk3 chk4

2用户chk1 chk2 chk3 chk4

I have an grid view. In which Template fields with checkboxes are there for add,view, Edit. If i check the textboxes then in database y value should be inserted otherwise N. my grid view is like this

Role_Id Role_Name Add View Edit Delete
1 Admin chk1 chk2 chk3 chk4
2 User chk1 chk2 chk3 chk4

推荐答案

从你的例子中我了解到你有4种可能的权利与用户角色相关联。所以我的建议是将一个名为Rights的单个int字段放入数据库,并通过使用存储在同一个int值中的bits字段来存储每个权限。

所以代替5(但最多可以是32或64取决于int type)你只能使用一个整数字段!



一些代码示例:

From your example I understood that you have 4 possible rights associated with user roles. So my suggestion is to have into a database a single "int" fields named "Rights" and to store each rights by using "bits fields" stored together in the same int value.
So in place of having 5 (but can be up to 32 or 64 depends on "int type") you could use only one integer field!

Some code example:
/// <summary>
        /// Sets/Clear the specified right into the given user rights flag.
        /// </summary>
        /// <param name="rightsFlag">The user rights flag.</param>
        /// <param name="right">The right.</param>
        /// <param name="value">For true set the right, and for false clear the right.</param>
        /// <returns>The modified user rights flag.</returns>
        private static int SetRightsFlagRight(int rightsFlag, ShopRights right, bool value)
        {
            int flagValue = (int)Math.Pow(2.0, (int)right);
            return (value ? (rightsFlag | flagValue) : (rightsFlag & ~flagValue));
        }

        /// <summary>
        /// Checks the specified right for the given user rights flag.
        /// </summary>
        /// <param name="rightsFlag">The user rights flag.</param>
        /// <param name="right">The right.</param>
        /// <returns>True if the user has the specified right.</returns>
        public static bool CheckRight(int rightsFlag, ShopRights right)
        {
            int flagValue = (int)Math.Pow(2.0, (int)right);
            return (rightsFlag & flagValue) == flagValue;
        }





您应该使用枚举字段来填写下一个字段:





And you should use an enum for bites fields like the next one:

public enum ShopRights
    {
        View = 0,
        Edit = 1,
        Add = 2,
        Delete = 3,
        // ...
    }


这篇关于如何在数据库中插入复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆