【发布时间】: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