c# - 填充 ComboBox(3 层架构)
问题描述
我对 3 层架构很陌生。我正在练习自己使用 3 层架构创建一个 WinForms 应用程序。
我有一个旧项目需要将其转换为 3 层架构。我对注册、登录、填充 Datagridview、选择数据没有任何问题。
我的问题是如何填充我的 ComboBox。
void FrmLoginLoad(object sender, EventArgs e)
{
Dropdowns.getRole(cmbUserRole);
}
下拉类
public static void getRole (ComboBox dd)
{
SQLHelper sqlConnect = new SQLHelper();
sqlConnect.DBConnection();
try
{
if (sqlConnect.con.State == ConnectionState.Closed) {
sqlConnect.con.Open();
}
SqlCommand cmd = new SqlCommand("SELECT Role FROM tbl_IT_RoleDescription",sqlConnect.con);
using (SqlDataReader dr = cmd.ExecuteReader()){
if (dr.HasRows) {
while (dr.Read()) {
dd.Items.Add(dr["Role"].ToString());
}
}
dr.Close();
}
sqlConnect.con.Close();
dd.SelectedIndex = 0;
}
catch(Exception ex)
{
MessageBox.Show("Error : " + ex.Message + "\n\nSend this issue to EUC Dev Team?", "Intake Tool", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
sqlConnect.con.Close();
}
}
我试图转换为 3 层架构, PL
private void frmLogin_Load(object sender, EventArgs e)
{
BLL_UserRole.getRole(cmbUserRole);
}
BLL
public void getRole(ComboBox cmbUserRole)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Role FROM tbl_IT_RoleDescription";
db.exeReader(cmd);
}
达尔
public DataTable exeReader(SqlCommand cmd)
{
DataTable dt = new DataTable();
try
{
cmd.Connection = getCon();
SqlDataReader dr;
dr = cmd.ExecuteReader();
dt.Load(dr);
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message + "\n\nSend this issue to EUC Dev Team?", "Intake Tool", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
con.Close();
}
return dt;
}
我试图对此进行研究,所有结果都是硬编码的,或者它们只是在 ComboBox 的集合属性中插入数据。但我想要的是 ComboBox 的数据来自数据库。
解决方案
我想我有一个解决方案给你。将 DataTable 对象返回到 PL 层会很棒。但是,如果您不想这样做,那么您可以检查我的虚拟代码并尝试一下。您也可以查看类似问题的链接LINK
public void getRole(ComboBox cmbUserRole)
{
DataSet myDataSet = new DataSet();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Role FROM tbl_IT_RoleDescription";
var temp=db.exeReader(cmd);
myDataSet.Tables.Add(temp);
cmbUserRole.DataSource = myDataSet.Tables["tbl_IT_RoleDescription"].DefaultView;
cmbUserRole.DisplayMember = "Role";
}
注意:请检查链接并让我知道它是否有效。
推荐阅读
- excel - 从 URL 获取图像文件尺寸
- python-2.7 - easy_install 破坏了我的 easy-install.pth 并将 python-commands 放在文件的开头和结尾
- android - Windows 10 中的元键盘键
- racket - 使用关键字参数定义函数
- python - Django:html形式的网址无法正常工作
- huffman-code - 使用霍夫曼编码压缩文件
- ios - NSFetchRequest 返回的获取的对象不在正确的部分
- f# - F# - 如何方便地将列表中的元素应用于柯里化函数的参数?
- python - TinyDB 在表行之间插入
- c - 如何获得矩阵乘法器 C 程序的未知矩阵的正确尺寸?