【问题标题】:set data from database and push it into "List" in Model class从数据库中设置数据并将其推送到模型类中的“列表”中
【发布时间】:2014-03-12 16:10:41
【问题描述】:

我可能遗漏了一些东西,或者可能不明白它是如何工作的,但我正在尝试从数据库中获取数据,将其推送到模型类(到属性中) - 列表中,然后从列表中获取值到 ComboBox .到目前为止我所做的是:

创建了获取“connstring”的 DBConnection 类,执行 sp,从数据库中读取数据。我还创建了一个模型,该模型具有一个属性“部门”,我想通过使用 comboBox1.Items.Add() 将值推送到 ComboBox...

我错过了什么?为什么会出现投射错误?

模型类:

namespace AddRequester
{
    public class ListDepartment
    {
        public List<string> Department { get; set; }
    }
}

DBConnection 类:

namespace AddRequester
{
    public class DBConnection
    {
        string conn = ConfigurationManager.ConnectionStrings["dbConfig"].ToString();
        public void listDepartment()
        {
            SqlConnection sql = new SqlConnection(conn);
            try
            {
                SqlCommand comm = new SqlCommand("ListDepartment", sql);
                comm.CommandType = CommandType.StoredProcedure;
                sql.Open();
                ListDepartment departs = new ListDepartment();
                using (var dr = comm.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        departs.Department = dr["Description"].ToString();
                    }
                }
            }
            catch (Exception e)
            { 
                //add output
            }
            sql.Close();
        }
    }
}

还有什么是从模型中获取值到组合框的最佳方法?

【问题讨论】:

    标签: database list model combobox


    【解决方案1】:

    由于没有人知道这样做很热,所以我以不同的方式完成了它。我正在共享代码,以便其他人遇到同样的问题时可以使用它。

    所以,我修改了 DBConnection 类的代码并完全删除了“模型”ListDepartment 类,

    namespace AddRequester
    {
        public class DBConnection
        {
    
            public List<string> listDepartment()
            {
                string conn = ConfigurationManager.ConnectionStrings["dbConfig"].ToString();
                SqlConnection sql = new SqlConnection(conn);
                List<string> departments = new List<string>();
                try
                {
                    SqlCommand comm = new SqlCommand("ListDepartment", sql);
                    comm.CommandType = CommandType.StoredProcedure;
                    sql.Open();
                    using (var dr = comm.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                           var dep = dr["Description"].ToString();
                           departments.Add(dep);
                        }
                    }
                }
                catch (Exception e)
                { 
                    Console.WriteLine(e.ToString());
                }
                sql.Close();
                return departments;
            }
        }
    }
    

    我已经添加到Form1.cs中

    namespace AddRequester
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
                DBConnection db = new DBConnection();
                foreach (var deps in db.listDepartment())
                {
                    comboBox1.Items.Add(deps);
                }
    
            }
        }
    }
    

    最后,当我启动应用程序并使用 combobox1 时,我会从数据库中获取项目列表。 干杯。

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2016-01-17
      • 2020-09-22
      • 1970-01-01
      • 2014-09-15
      • 2018-11-09
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      相关资源
      最近更新 更多