如何在按钮clik上数据库表中的多个复选框值 [英] How to savel multiple checkbox values in database table on button clik

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

问题描述

我使用了4个不同的复选框我希望将它们保存在数据库中点击按钮

怎么做这个.Pase帮助我。任何人谢谢..



我尝试过:



i我不使用此代码我不知道我是如何粘贴此代码的只有形式。

请看我的简单问题并帮助我



i am used 4 diffrent checkbox i want save them in database on button click
how done this.Pease Help me.anyone Thank you..

What I have tried:

i am not use this code i dont know how do this i am paste this code for only formalty.
Please see my simple question and help me

for (int i = 0; i <=CheckBoxList1.Items.Count-1; i++)
   {

       if (CheckBoxList1.Items[i].Selected)
       {

           if (str == "")
           {
               str = CheckBoxList1.Items[i].Text;
           }
           else
           {
               str += "," + CheckBoxList1.Items[i].Text;

           }

       }
   }
   con.Open();
   SqlCommand cmd = new SqlCommand("insert into aa(a)values('" +str + "')", con);
   cmd.ExecuteNonQuery();

推荐答案

那么你想为价值保存什么?有点字段(好主意),单词True或False,Yes或No(不好的主意)



你发布的代码没有做非常好,你显然已经走向了SQL注入的道路 - SQL Injection - OWASP [< a href =https://www.owasp.org/index.php/SQL_Injectiontarget =_ blanktitle =New Window> ^ ]。使用正确的技术 - 注射预防备忘单 - OWASP [ ^ ]。使用参数化查询!



您还要构建一个以逗号分隔的复选框文本列表,而不是值。即使您使用正确的属性存储任何内容作为数据库中的逗号分隔列表也是一个坏主意。给每个文本框提供它自己的列...这样就可以更容易地将数据输入和输出数据库。



在这个的右下角页面是相关问题的列表 - 首先尝试解决方案(这部分是我们所说的做一些研究),写一些正确的代码并试一试。如果您在回来后仍然遇到问题(使用您的密码),我们会尽力帮助您。



当您回来时,请避免使用 plzz hlp me。这些词是请和帮助 - 并非每个人都将英语作为他们的第一语言,并且使用文字说话只会让他们更难理解你的问题 - 这意味着他们将无法(或倾向于)帮助您
So what do you want to save for the value? A bit field (good idea), the words "True" or False, "Yes" or "No" (bad ideas)

The code you posted is not doing a great deal of good, you are clearly heading down the road to SQL Injection land - SQL Injection - OWASP[^]. Use the correct techniques - Injection Prevention Cheat Sheet - OWASP[^]. Use parameterised queries!

You are also building up a comma-separated list of the checkbox text not the values. Even if you were using the right property storing anything as a comma-separated list in a database is a Bad Idea. Give each text box it's own column.. it is much, much easier to get information in and out of the database that way.

In the bottom right corner of this page is a list of "Related Questions" - try the solutions there first (that is partly what we mean by "do some research"), write some proper code and try it out. If you are still having problems after that come back (with your code) and we will try to help you further.

When you do come back avoid using terms like "plzz hlp me". The words are "please" and "help" - not everyone has English as their first language and using text-speak just makes it harder for them to understand your question - and that means they won't be able to (or inclined to) help you


这里是使用多个checkBox在数据库表中保存值的解决方案



Here the solution for using multiple checkBox to save values in your databse table

String activity = "";

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





在插入查询之前在按钮上使用此代码。

字符串活动是为你的Checkbox定义。你可以使用活动的任何名称实例,然后在插入查询中使用这个不同的活动。

喜欢这个



use this code on your button before insert query.
"String activity" is define for your Checkbox.you can use any name instance of "activity" then you use this difened activity in your insert query.
Like this

String update = "update StdRegistration_db set Name= @name,ParentsName= @parentsname,DOB= @DOB,Gender=@Gender,ContactsNo= @contacts ,EmailID= @email,Address=address @,<big>Hobbies= @activity</big> where id=@id";


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

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