【发布时间】:2014-09-23 02:50:09
【问题描述】:
我正在使用 3 层架构设计 Windows 应用程序。我的一个表单包含动态生成的控件。我的问题是如何从我的数据库中的控件中保存多个值。我使用了 return 语句,但因为它只返回一个值,所以我的数据库只存储了每个控件的一个值。基本上我正在创建 BILL FORM。
我为每个控件创建了 8 个方法,并将它们的值传递到业务逻辑层的函数中,但是如上所述,它只保存了每个控件的一个值,而不是保存多个值。我也尝试使用元组,但它给了我“转换异常”
我的 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");
}
}
【问题讨论】: