繁体   English   中英

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

[英]How to save multiple values from various controls in sql server database?

我正在使用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,或者如果控件是动态创建的,则不应按其名称引用它们,依此类推,但希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM