如何从SQL Server数据库的各种控件中保存多个值?

肛门·卡莱姆(Anus Kaleem)

我正在使用3层体系结构设计Windows应用程序。我的表单之一包含动态生成的控件。我的问题是如何从数据库中的控件中保存多个值。我使用过return语句,但是由于它仅返回一个值,因此我的数据库仅存储了每个控件中的一个值。基本上我是在创建帐单。

我为每个控件创建了8个方法,并在业务逻辑层的函数中传递了它们的值,但是如上所述,与其保存多个值,不如每个控件仅保存一个值。我也尝试使用Tuples,但随后它给了我“转换异常”

我的8种方法之一是:

 public string combo_box_1()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is ComboBox)
                {
                    if (ctrl.Name.Equals("combo_box_1"))
                    {
                        string main_category = ctrl.Text;
                        string[] arr1 = { main_category };
                        foreach (string alph in arr1)
                        {
                            MessageBox.Show(alph);
                            return alph;
                        }
                        return main_category;

                    }
                }
            }
            return null;
        }

它是元组版本:

public Tuple<string> combo_box_1()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is ComboBox)
                {
                    if (ctrl.Name.Equals("combo_box_1"))
                    {
                        string main_category = ctrl.Text;
                        Tuple<string> txt2 = new Tuple<string>(main_category);
                        return txt2;
                    }
                }
            }
            return null;
        }

我的业务层函数(元组版本):

public bool save_bill(Tuple<string> CB1, Tuple<string> CB2, Tuple<string> CB3, Tuple<string> CB4, Tuple<string> NUD1, Tuple<string> CB5, Tuple<string> TB1, Tuple<string> TB2)
        {
            try
            {
                DAL obj = new DAL();
                obj.OpenConnection();
                obj.LoadSpParameters("save_bill", CB1, CB2, CB3, CB4, NUD1, CB5, TB1, TB2);
                obj.ExecuteQuery();
                obj.UnLoadSpParameters();
                obj.CloseConnection();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

我的保存按钮后面的程序(元组版本):

private void save_button_Click(object sender, EventArgs e)
        {
            BLL obj = new BLL();
            bool obj2 = obj.save_bill(combo_box_1(), combo_box_2(), combo_box_3(), combo_box_4(), numeric_up_down_1(), combo_box_5(), txt_box_1(), txt_box_2());
            if (obj2 == true)
            {
                MessageBox.Show("bill saved");
            }

            else
            {
                MessageBox.Show("bill cannot be saved");
            }

        }
奥托

我必须承认我在理解您的代码时遇到了麻烦,但是我认为这可能会有所帮助:更改save_bill方法以接受字符串参数,不需要Tuples

public bool save_bill(Tuple<string> CB1, Tuple<string> CB2, Tuple<string> CB3
    , Tuple<string> CB4, Tuple<string> NUD1, Tuple<string> CB5, Tuple<string> TB1
    , Tuple<string> TB2)

public bool save_bill(string CB1, string CB2, string CB3, string CB4
, string NUD1, string CB5, string TB1, string TB2)

而不是为每个控件使用8(奇怪)方法

bool obj2 = obj.save_bill(combo_box_1(), combo_box_2(), combo_box_3()
, combo_box_4(), numeric_up_down_1(), combo_box_5(), txt_box_1(), txt_box_2());

使用它并直接访问它们的值

bool obj2 = obj.save_bill(combo_box_1.Text, combo_box_2.Text
, combo_box_3.Text, combo_box_4.Text, numeric_up_down_1.Value.ToString()
, combo_box_5.Text, txt_box_1.Text, txt_box_2.Text);

有一些问题,例如您是否真的需要ComboBox.Text而不是SelectedID,或者如果控件是动态创建的,则不应按其名称引用它们,依此类推,但希望对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在SQL Server中保存数据库默认位置

如何在数据库的单个列中保存多个复选框值并使用laravel检索它

如何使用SQL数据库在Android中保存从Arduino开发板收到的数据?

如何在SQL数据库中保存Unicode字符

如何在MySQL数据库的laravel中保存多个输入?

如何在SQL Server数据库中保存文本和图像的组合

如何在实时数据帧中保存sql数据库?闪亮的

如何在Django的数据库中保存值?

如何在数据库中保存XML(writer)

如何从SQL Server数据库检索值?

如何在SQL数据库中保存多个选项?

如何允许在SQL数据库中保存重复键

如何在SQL数据库中保存列表?

rails如何在数据库的日期字段中保存date_select字段值

如何在数据库中保存跟踪折线?

如何在sqlite数据库中保存每个微调器值?

如何在数据库中保存Blob文件

DateTimePicker 值未保存到 SQL Server 数据库

如何在sqlite数据库中保存来自微调器的值

如何通过 ssms 或 c# 在 SQL Server 2016 数据库中保存 Excel 文件?

如何在数据库中保存一个特定值

如何将多个动态文本框值保存到 SQL Server 数据库中

如何在数据库中保存多个下拉值?

如何在数据库的不同行中保存多个复选框值

使用 Python 在 SQL Server 数据库中保存文件时保留 XML 版本

如何使用 C# 在 Azure SQL 数据库中保存和存储数据

如何在laravel的数据库中保存用户帖子?

在 SQL 数据库中保存值的函数

如何防止在 SQLite 数据库中保存空数据