如何检索多个复选框值并在此处检查我在单列数据库中存储了复选框值 [英] How To Retrieve Multiple Checkbox Values And Get Checked Here I Stored Checkbox Values In Single Column Database

查看:76
本文介绍了如何检索多个复选框值并在此处检查我在单列数据库中存储了复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样插入



这是插入的代码

I inserted like this

Here is the inserted code

private void button1_Click(object sender, EventArgs e)
       {
           string s = "";
           foreach(Control cc in this.Controls)
           {
               if (cc is CheckBox)
               {
                   CheckBox a = (CheckBox)cc;
                   if (a.Checked)
                   {
                       s = a.Text  + s;
                   }
               }
           }
           cn.Open();
           SqlCommand cmd = new SqlCommand("insert into ComboBox(id,hobbies) values('"+textBox1.Text+"','"+s+"')",cn);
           cmd.ExecuteNonQuery();
           cn.Close();
           MessageBox.Show("sved Successfully");
       }



现在我想从数据库中检索数据......但我无法从数据库中检索数据实际上我从数据库中获取数据..但价值没有得到检查..代码是




and now i want to retrieve data from database...but i was unable to retrieve data from database actually i am getting data from database..but values are not getting checked..here is the code

private void button2_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
checkBox4.Checked = false;
cn.Open();
SqlCommand cmd1 = new SqlCommand("select * from ComboBox where id='"+textBox1.Text+"'",cn);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
string aa=dr["hobbies"].ToString();
string[] a = aa.Split();
foreach (Control cc in this.Controls)
{
if (cc is CheckBox)
{
CheckBox b = (CheckBox)cc;
for (int j = 0; j < a.Length; j++)
{
if (a[j] == b.Text)
{
b.Checked = true;
}
}
}
}
}
dr.Close();
cn.Close();
}



你可以帮我这个吗?

它没有通过foreach循环它出来可以帮助我这个??


can u help me with this??
its not going through foreach loop its getting out can u help me with this??

推荐答案

不要将用户输入的字符串连接到sql查询中。您将自己置于 SQL注入的高风险中[ ^ ](该链接还包含有关如何解决问题的建议)



当您将数据存储在数据库中时,您只是连接每个复选框中的文本 - 那里没有分隔符。所以 aa.Split()返回一个值(完整的连接字符串)。它永远不会匹配任何单个复选框的文本,因此不会检查它们。



您可以在每个
Do not concatenate strings from user input into your sql queries. You place yourself at high risk of SQL Injection[^] (the link also includes suggestions on how to address the issue)

When you are storing your data on the database you are just concatenating the text from each checkbox - there are no separators in there. So the aa.Split() is returning a single value (the full concatenated string). It will never match the text of any single checkbox therefore none of them will be checked.

You could put a separator between each
s = a.Text  + s + ",";

并将分隔符传递给Split

and pass the separator into the Split

string[] a = aa.Split(',');

但说实话这个设计不好。 (对于不好,读非常糟糕)。



如果您知道前面的复选框数量,那么在数据库中为每个复选框都有一列(使用BIT数据类型)。如果复选框的数量是可变的,则每个复选框都有一行(注意您当前使用的ID在这种情况下不能是唯一的)

but to be honest this design is not good. (For "not good" read "really bad").

If you know the number of checkboxes up front then have a column for each of them on the database (using a BIT datatype). If the number of checkboxes is variable then have a row per checkbox (note the Id you currently use cannot be unique in that case)


这篇关于如何检索多个复选框值并在此处检查我在单列数据库中存储了复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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