简体   繁体   English

保存/检索/比较DB中的CheckboxList值

[英]Save/Retrieve/Compare CheckboxList values from DB

I am not sure if what i want is possible, but so far i have had no luck... 我不确定我想要的是否可能,但到目前为止我没有运气......

I am using Visual Studio, asp.net and SQL. 我正在使用Visual Studio,asp.net和SQL。 a field in table in varchar(max) type... varchar(max)类型表中的字段...

What I want to do is save the values of CheckBoxList into the database and retrieve them to compare them in a SQL command to filter a few values from the table in database. 我想要做的是将CheckBoxList的值保存到数据库中并检索它们以在SQL命令中比较它们以从数据库中的表中过滤掉一些值。

It's easier to understand when I give an example: 当我举一个例子时,它更容易理解:

So I have these in HTML side of the code. 所以我在代码的HTML方面有这些。

<asp:CheckBoxList ID="CheckBoxList1" runat="server" onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
        <asp:ListItem Value="1">Sales</asp:ListItem>
        <asp:ListItem Value="2">Administration</asp:ListItem>
        <asp:ListItem Value="3">Help</asp:ListItem>
</asp:CheckBoxList>

This in code to save the data 这在代码中保存数据

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
        TextBox20.Text = "";
        string s = null;

        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
                s += CheckBoxList1.Items[i].Value + ",";

            TextBox20.Text = s;
        }
    }

Now when some one chooses like two options, example Sales and Administration , I want the values 1,2 to be stored in the database in a single column. 现在当某人选择两个选项时,例如SalesAdministration ,我希望将值1,2存储在单个列的数据库中。

Then retrieve the values, using SQL show rows which have Sales or Administration / Sales and Administration in the column I want. 然后使用SQL show行检索值,这些行包含我想要的列中的Sales或Administration / Sales和Administration。

I am not sure whether I am going about this the right way.. But any help would be appreciated.... I read that creating a table with all the values i need is they correct way.. but i dunno how it works.. 我不确定我是否会以正确的方式解决这个问题。但是任何帮助都会受到赞赏....我读到创建一个包含我需要的所有值的表是正确的方式..但我不知道它是如何工作的。 。

Firstly, I would recommend the use of the StringBuilder class for doing string concatenation inside a loop as it is much more efficient - 首先,我建议使用StringBuilder类在循环内进行字符串连接,因为它更有效 -

For routines that perform extensive string manipulation (such as apps that modify a string numerous times in a loop), modifying a string repeatedly can exact a significant performance penalty. 对于执行大量字符串操作的例程(例如在循环中多次修改字符串的应用程序),重复修改字符串可能会导致严重的性能损失。 The alternative is to use StringBuilder, which is a mutable string class. 另一种方法是使用StringBuilder,它是一个可变的字符串类。

MSDN StringBuilder Class Documentation MSDN StringBuilder类文档

I agree with the comment above made by HABO, storing comma seperated values in a single column is almost always a bad idea. 我同意HABO上面的评论,将逗号分隔值存储在一个列中几乎总是一个坏主意。 You should probably create a class called User. 您应该创建一个名为User的类。 Give the User class properties such as 'name' and 'roles'. 提供User类属性,例如“name”和“roles”。

With regards to the database structure for this data, once again I agree with HABO, two tables would be recommended. 关于这些数据的数据库结构,我再次同意HABO,建议使用两个表。 User and UserRoles. 用户和用户角色。 In the User table you would store data such as Id, Username, Age, etc. you can then in the UserRoles table store UserId (the Id from the User table), in this way a User can be assigned multiple roles. 在User表中,您将存储Id,Username,Age等数据,然后您可以在UserRoles表存储UserId(User表中的Id)中,这样可以为User分配多个角色。

If you want more detail feel free to ask and I'll try and provide example schema etc. 如果你想要更多的细节随时问我,我会尝试提供示例架构等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM