如何在C#中的数据库中的单列中存储和检索多个复选框 [英] How store and retrieve multiple checkbox in single column in database in C#

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

问题描述

我有12个复选框,复选框文本是月份名称,例如jan,feb,march .....

i想要,如果选中其中一些复选框,则在单列中选中文本存储的值,然后如果用户ID再次搜索,则检索此。



我的数据库代码

I have 12 checkbox the checkbox text is months name e.g jan,feb,march.....
i want if some of these checkbox is checked the value of text store in single column and then retrieve this if the user id search again.

my database code

using (DbSQLServer db = new DbSQLServer(AppSetting.ConnectionString()))
{
    db.SaveOrUpdateRecord("Fee_Details", GetData());
}



然后在GetData函数中设置值


and then set values in GetData funtion

private MonthlyFeeStractures GetData()
       {
           MonthlyFeeStractures feeRecord = new MonthlyFeeStractures();
           feeRecord.StudentName = StudentNameLbl.Text;
           feeRecord.FatherName = FatherName.Text;
           feeRecord.RegistrationNumber = RegistrationTextBox.Text;
           feeRecord.ClassOfAdmission = FindStudentComboBox.Text;
           feeRecord.DateOfPay = FeePaymentDateDateTimePicker.Value;
           feeRecord.PaymentType = PaymentTypeComboBox.Text;
           feeRecord.FeeMonth = checkboxdata();
           feeRecord.Fee = FeeLabel.Text;
           feeRecord.Dues = DuesLabel.Text;
           feeRecord.Quantity = Convert.ToInt32(QuantityLabel.Text);
           feeRecord.TotalPayment = TotalAmountLabel.Text;
           feeRecord.Discount = DiscountTextBox.Text;
           feeRecord.PayBy = PaidByTextBox.Text;
           feeRecord.PromotionFee = PromoFeeTextBox.Text;
           feeRecord.PromotionFee = PromoClassCmoboBox.Text;
           feeRecord.ReceiptNumber = ReceiptNumberLabel.Text;

           return feeRecord;
       }





其他数据存储正确但复选框名称不存储,当我调试此



Other Data Store Correctly But The checkbox name is not store and when i Debug on this

feeRecord.FeeMonth = checkboxdata();



函数返回s的值但是为null



什么我试过了:




the function return the vale of s but is is null

What I have tried:

private string checkboxdata()
        {
            string s =Convert.ToString(0);
            foreach (Control c in MonthlyFee.Controls)
            {                            
                     CheckBox cb = c as CheckBox;
                    if (cb.Checked)
                    {
                        s = cb.Text ;
                    }                
            }
            return s;
            
        }

推荐答案

引用:

如何在C#中的数据库中的单列中存储和检索多个复选框

How store and retrieve multiple checkbox in single column in database in C#



基本上有3个解决方案:

- 每个值使用1个字段

- 每个值使用1行

- 将所有复选框合并为1个值

您的选择。



您的代码:

- 不要检查复选框是否是几个月。

- 只获取上次复选的复选框的名称。

- 缺少在数据库中保存数据的部分。


You have basically 3 solutions:
- use 1 field per value
- use 1 row per value
- combine all checkbox in 1 value
your choice.

Your code:
- do not check if checkbox are months or not.
- only get the name of last checked checkbox.
- is missing the part that save data in database.


没有更多细节,我不能更具体,但这应该给你一些想法:
Without more detail, I can't be more specific, but this should give you some ideas:
StringBuilder sb = new StringBuilder();

foreach (CheckBox ckbx in MonthlyFee.Controls
    .OfType<CheckBox>()
    .Where(cbx => cbx.Checked))
{
    sb.AppendLine(ckbx.Name);
}

string checkedmonths = sb.ToString();


写一些与s有关的数据库代码......然后再提出更多问题。
Write some database code that does something with "s" ... then come back with more questions.


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

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