从数据 table 添加到字典记录
Adding to dictionary records from data table
我是 MVVM 的新手 pattern.I 已经创建了 MVVM 模式,我必须在其中将列表填充到树视图 control.I 有视图、视图模型并且 Model.I 已经在模型中填充了数据通过硬编码。但现在我想连接我的 MS Access 数据 table 以在模型中列出而不是硬编码 data.I 已经创建了 DAL lier 并连接了我的 MS Access 数据 table 如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
namespace DAL2
{
public class PersonalDAL
{
// static String _ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\faisal\Xerox2014\wpf\WPFHierarchy\IconnectDB.accdb");
public DataTable loademp( )
{
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter("select * from empTable", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
return dt;
}
}
}
此处 loademp 方法将 return 数据 table。
在模型中,我只是想填充列表。我被卡住的代码在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DAL2;
using System.Data;
namespace DevLake.OrgChart.UI.Model
{
class OrgChartManager
{
private static OrgChartManager self;
//orgchart stored in dictionary
private Dictionary<int, Node> list = new Dictionary<int, Node>();
private OrgChartManager()
{
DataTable my_datatable = new DataTable();
my_datatable = new PersonalDAL().loademp();
int ColumnCount = my_datatable.Columns.Count;
foreach (DataRow dr in my_datatable.Rows)
{
list.Add(dr);
//******here I get stuck and throw syntax error on this line
}
//hard coded populate data
// list.Add(1, new Node { Id = 1, FirstName = "Joe", LastName = "Smith", ParentId = 1 });
//list.Add(2, new Node { Id = 2, FirstName = "Rich", LastName = "Angel", ParentId = 1 });
//list.Add(3, new Node { Id = 3, FirstName = "Mary", LastName = "Peach", ParentId = 1 });
这里我无法填写列表,它抛出语法错误。任何人都可以帮助我如何用数据 table 数据填充我的列表。我的 MS Access table 看起来像这样:
ID FirstName LastName ParentId
1 Joe Smith 1
10 Grey Peech 9
11 Manny Travel 9
2 Rich Angel 1
3 Mary Peach 1
4 Mike Door 2
5 Jimmy Fond 2
6 Ann Brown 4
7 George Farm 4
8 Able Jump 4
9 Corn Shaw 1
我能够连接数据库并将数据 table 带到我的模型中,但是将数据 table 数据填充到列表中是我遇到困难的地方..请帮助我的朋友们
我的节点class:
class Node
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int ParentId { get; set; }
}
}
Drow donw item contro within the data contarol
https://scikit-learn.org/stable/
foreach (DataRow dr in my_datatable.Rows)
{
Node node = new Node
{
Id = (int)dr["ID"]
FirstName = (string)dr["FirstName"],
LastName = (string)dr["LastName"],
ParentId = (int)dr["ParentId"]
};
list[node.Id] = node;
}
我是 MVVM 的新手 pattern.I 已经创建了 MVVM 模式,我必须在其中将列表填充到树视图 control.I 有视图、视图模型并且 Model.I 已经在模型中填充了数据通过硬编码。但现在我想连接我的 MS Access 数据 table 以在模型中列出而不是硬编码 data.I 已经创建了 DAL lier 并连接了我的 MS Access 数据 table 如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Configuration;
using System.Data;
namespace DAL2
{
public class PersonalDAL
{
// static String _ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\faisal\Xerox2014\wpf\WPFHierarchy\IconnectDB.accdb");
public DataTable loademp( )
{
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter("select * from empTable", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
return dt;
}
}
}
此处 loademp 方法将 return 数据 table。 在模型中,我只是想填充列表。我被卡住的代码在这里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DAL2;
using System.Data;
namespace DevLake.OrgChart.UI.Model
{
class OrgChartManager
{
private static OrgChartManager self;
//orgchart stored in dictionary
private Dictionary<int, Node> list = new Dictionary<int, Node>();
private OrgChartManager()
{
DataTable my_datatable = new DataTable();
my_datatable = new PersonalDAL().loademp();
int ColumnCount = my_datatable.Columns.Count;
foreach (DataRow dr in my_datatable.Rows)
{
list.Add(dr);
//******here I get stuck and throw syntax error on this line
}
//hard coded populate data
// list.Add(1, new Node { Id = 1, FirstName = "Joe", LastName = "Smith", ParentId = 1 });
//list.Add(2, new Node { Id = 2, FirstName = "Rich", LastName = "Angel", ParentId = 1 });
//list.Add(3, new Node { Id = 3, FirstName = "Mary", LastName = "Peach", ParentId = 1 });
这里我无法填写列表,它抛出语法错误。任何人都可以帮助我如何用数据 table 数据填充我的列表。我的 MS Access table 看起来像这样:
ID FirstName LastName ParentId
1 Joe Smith 1
10 Grey Peech 9
11 Manny Travel 9
2 Rich Angel 1
3 Mary Peach 1
4 Mike Door 2
5 Jimmy Fond 2
6 Ann Brown 4
7 George Farm 4
8 Able Jump 4
9 Corn Shaw 1
我能够连接数据库并将数据 table 带到我的模型中,但是将数据 table 数据填充到列表中是我遇到困难的地方..请帮助我的朋友们
我的节点class:
class Node
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int ParentId { get; set; }
}
}
Drow donw item contro within the data contarol
https://scikit-learn.org/stable/
foreach (DataRow dr in my_datatable.Rows)
{
Node node = new Node
{
Id = (int)dr["ID"]
FirstName = (string)dr["FirstName"],
LastName = (string)dr["LastName"],
ParentId = (int)dr["ParentId"]
};
list[node.Id] = node;
}