簡體   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