下面的过程使用数据库中的值填充组合框。
还有一个用于ListBoxes,它与完全相同,除了“ box”是一个ListBox。
CB和LB类都具有Items,并且都继承了没有Item的ListControl。
如何摆脱那里的重复代码?
private void UpdateBox (ComboBox box,string select,string from,string order = "")
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
if (order == "") order = select;
using (SqlCommand command = new SqlCommand("SELECT " + select +
" FROM " + from + " ORDER BY " + order,conn))
{
SqlDataReader dataReader = command.ExecuteReader();
box.Items.Clear();
while (dataReader.Read())
{
box.Items.Add(dataReader[select]);
}
}
}
}
这里是另一个:
private void UpdateBox (ListBox box,conn))
{
SqlDataReader dataReader = command.ExecuteReader();
box.Items.Clear();
while (dataReader.Read())
{
box.Items.Add(dataReader[select]);
}
}
}
}