【发布时间】:2011-05-29 15:44:30
【问题描述】:
public List<Item> ListItemCollection;
String connString = "SERVER=;" +
"DATABASE=;" +
"UID=;" +
"PASSWORD=;";
private void GetItems()
{
String query = @"myQuery";
DataTable theTable = new DataTable();
using (MySqlConnection conn = new MySqlConnection(connString))
{
MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
MySqlCommand command = new MySqlCommand(query);
da.Fill(theTable); // here all works fine.
using (MySqlDataReader reader = command.ExecuteReader())
{
/// At this point throws the exception .....
// Loop through each record.
while (reader.Read())
{
ListItemCollection.Add(new Item());
}
}
}
}
theTable.Columns.Clear();
theTable.Rows.Clear();
theTable.Clear();
ListItemCollection.Items.Clear();
DataContext = ListItemCollection;
ListItemCollection.Items.Refresh();
内部异常.. {“连接必须有效且打开。”}
如果我使用 DataAdapter 并填充 dataTable "da.Fill(theTable)" 事情会非常顺利,也许有办法将数据从 DataTable 传输到 List?表.列.列表?
这一切的最终目的是,我想将分组添加到 ListControl(ListBox、DataGrid、ListView),在 XAML 中设置 DataContext 代码隐藏和绑定 ItemSource。或许你可以对数据表的列表进行分组?
【问题讨论】:
标签: .net wpf database list datatable