在SQL Server中存储复选框列表的最佳方法是什么 [英] What is the best way to store a checkbox list in SQL server

查看:66
本文介绍了在SQL Server中存储复选框列表的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的复选框列表



< asp:CheckBoxList ID =chkBoxrunat =server>

< asp:ListItem Text =TextValue =Text/>

< asp:ListItem Text =EmailValue =Email/>

< asp:ListItem Text =MailValue =Mail/>

< asp:ListItem Text =CalendarValue =Calendar/> < br $> b $ b



我不确定在我的SQL数据库中存储它的最佳方法是什么。我知道我可以查看附加到字符串的值。



但是如果我想重新加载这个表格并且选中了复选框,自动检查,我应该如何将这些信息存储在我的数据库中?



我尝试了什么:



以下是我将这些值附加到字符串的方法



string interests = string.Empty;

foreach(this.chkBox.Items中的ListItem项目)

if(item.Selected)

兴趣+ = item +,;



但我不认为这是理想的。我真的不知道如何将其从表中拉出来并将其显示在表格中。

I have a checkbox list that looks like this

<asp:CheckBoxList ID="chkBox" runat="server">
<asp:ListItem Text="Text" Value="Text"/>
<asp:ListItem Text="Email" Value="Email"/>
<asp:ListItem Text="Mail" Value="Mail"/>
<asp:ListItem Text="Calendar" Value="Calendar"/>


I'm not sure what the best way to store this in my SQL database is. I know I can look through the values to append to a string.

But if I want to reload this form and have the checkbox's that were checked off, checked automatically, how should I store this info in my database?

What I have tried:

Here is how I appended these values to a string

string interests = string.Empty;
foreach (ListItem item in this.chkBox.Items)
if (item.Selected)
interests += item + ",";

But I don't think this is ideal. I don't really know how i would pull this out from my table and display it in the form.

推荐答案

当您在数据库中存储项目时,您不仅需要存储项目,但也存储其检查状态。为了达到这个目的,在数据​​库中使用'bit'columntype的最佳方法是根据你的列表框检查/未检查状态存储真值或假值

了解如下链接以获取更多详细信息

将多个值从CheckBoxList插入到ASP中的数据库。净 [ ^ ]
When you store items in database you not only need to store items but also store their checked state. to achieve this, best way to use 'bit' columntype in database it will store true or false value depending upon your checked/unchecked state of listbox
lookout below link for more details
Insert multiple values from CheckBoxList to Database in ASP.Net[^]


看看这些链接希望这些帮助

如何使用c#将复选框值存储到sql server数据库中 - Stack Overflow [ ^ ]

如何在sqlserver中存储多个选择的Checkboxlist ASP.NET论坛 [ ^ ]
Take a look at these links hope these helps
How to store a checkbox value into sql server database using c# - Stack Overflow[^]
How to store multiple selection of Checkboxlist in sqlserver | The ASP.NET Forums[^]


这篇关于在SQL Server中存储复选框列表的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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