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

时间:2021-12-03 01:40:20

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. a field in table in varchar(max) type...

我使用的是Visual Studio、asp.net和SQL。表中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.

现在,当有人选择两个选项时,例如Sales和Administration,我希望将值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..

我不知道我是否走对了路。但任何帮助将不胜感激....我读到创建一个包含所有我需要的值的表是正确的。但是我不知道它是怎么运作的。

1 个解决方案

#1


1  

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. You should probably create a class called User. Give the User class properties such as 'name' and 'roles'.

我同意HABO上面的注释,在单个列中存储逗号分隔值几乎总是一个坏主意。您可能应该创建一个名为User的类。给出用户类属性,如“名称”和“角色”。

With regards to the database structure for this data, once again I agree with HABO, two tables would be recommended. 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.

关于这个数据的数据库结构,我再次同意HABO,建议使用两个表。用户和UserRoles。在User表中,您将存储Id、用户名、年龄等数据,然后可以在UserRoles表中存储UserId(来自用户表的Id),这样用户就可以被分配多个角色。

If you want more detail feel free to ask and I'll try and provide example schema etc.

如果您想了解更多细节,请随意提问,我将尝试提供示例模式等。

#1


1  

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. You should probably create a class called User. Give the User class properties such as 'name' and 'roles'.

我同意HABO上面的注释,在单个列中存储逗号分隔值几乎总是一个坏主意。您可能应该创建一个名为User的类。给出用户类属性,如“名称”和“角色”。

With regards to the database structure for this data, once again I agree with HABO, two tables would be recommended. 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.

关于这个数据的数据库结构,我再次同意HABO,建议使用两个表。用户和UserRoles。在User表中,您将存储Id、用户名、年龄等数据,然后可以在UserRoles表中存储UserId(来自用户表的Id),这样用户就可以被分配多个角色。

If you want more detail feel free to ask and I'll try and provide example schema etc.

如果您想了解更多细节,请随意提问,我将尝试提供示例模式等。