public class InnovationSubmission
{
public OleDbConnection connectr = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabaseTEST.accdr;Jet OLEDB:Database Password=DWPOIT");
public OleDbDataAdapter Adaptor = new OleDbDataAdapter();
private List<SelectListItem> _Area = new List<SelectListItem>();
private List<SelectListItem> _Team = new List<SelectListItem>();
public int ID { get; set; }
[Required(ErrorMessage="Please enter a short description")]
public string ShortDescription { get; set; }
[Required(ErrorMessage = "Please enter a detailed description")]
public string DetailedDescription { get; set; }
public string Status { get; set; }
[Required(ErrorMessage = "Please enter your name")]
public string Originator { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(".+\\@.+\\..+",
ErrorMessage = "Please enter a valid email address")]
public string OriginatorEmail { get; set; }
[Required]
public string SelectedArea { get; set; }
[Required]
public string SelectedTeam { get; set; }
public List<SelectListItem> Area
{
get { return _Area; }
}
public List<SelectListItem> Teams
{
get
{
string SQLSelect = ("SELECT * FROM Teams");
OleDbCommand sCommand = new OleDbCommand(SQLSelect, connectr);
Adaptor.SelectCommand = sCommand;
connectr.Open();
Adaptor.Fill(areaTable);
connectr.Dispose();
foreach (DataRow row in areaTable.Rows)
{
_Team.Add(new SelectListItem() { Text = row[1].ToString(), Value = row[1].ToString() });
}
return _Team;
}
}
public DataTable areaTable = new DataTable();
}
}
答案 0 :(得分:1)
对我来说,看起来你的连接器对象有问题。可能它在其他地方使用,这就是你得到例外的原因。打开新连接,执行查询并关闭它。尝试使用using
在使用块中实现该目标并进行查询。
另外,正如评论中已经提到的那样 - 在属性getter中进行db查询并不是一个好主意。看起来很奇怪和hacky ......
答案 1 :(得分:-1)
如果我像这样使用它就可以了!
public List<SelectListItem> Teams
{
get
{
string SQLSelect = ("SELECT * FROM Teams");
OleDbCommand sCommand = new OleDbCommand(SQLSelect, connector);
Adaptor.SelectCommand = sCommand;
connector.Open();
Adaptor.Fill(areaTable);
connector.Dispose();
foreach (DataRow row in areaTable.Rows)
{
_Team.Add(new SelectListItem() { Text = row[1].ToString(), Value = row[1].ToString() });
}
return _Team;
}
}