如何讀取SQL數據以獲取分層單元列表?將sql層次讀入c#對象
不取決於僅SQL Server解決方案?
public class Unit {
public Unit Parent { get; set; }
public int Id { get; set; }
public String Name { get; set; }
}
List<Unit> list = new List<Unit>();
while(reader.Read())
{
// read sql data into clr object UNIT
}
該表有3列:
Id| ParentId | Name
1 | Null | bla
2 | 1 | x
3 | 1 | y
4 | 2 | z
5 | 2 | test
UPDATE:
That is the code which is taken from user marc_s:
List<Unit> units = new List<Unit>();
String commandText =
@";WITH Hierarchy AS
(
SELECT
ID, ParentID = CAST(NULL AS INT),
Name, HierLevel = 1
FROM
dbo.Unit
WHERE
ParentID IS NULL
UNION ALL
SELECT
ht.ID, ht.ParentID, ht.Name, h1.HierLevel + 1
FROM
dbo.Unit ht
INNER JOIN
Hierarchy h1 ON ht.ParentID = h1.ID
)
SELECT Id, ParentId, Name
FROM Hierarchy
ORDER BY HierLevel, Id";
using(SqlConnection con = new SqlConnection(_connectionString))
using (SqlCommand cmd = new SqlCommand(commandText, con))
{
con.Open();
// use SqlDataReader to iterate over results
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
// get the info from the reader into the "Unit" object
Unit thisUnit = new Unit();
thisUnit.Id = Convert.ToInt32(rdr["Id"]);
thisUnit.UnitName = rdr["Name"].ToString();
// check if we have a parent
if (rdr["ParentId"] != DBNull.Value)
{
// get ParentId
int parentId = Convert.ToInt32(rdr["ParentId"]);
// find parent in list of units already loaded
// NOTE => not needed anymore => Unit parent = units.FirstOrDefault(u => u.Id == parentId);
// Instead use this method to find the parent:
Unit parent = FindParentUnit(units, parentId);
// if parent found - set this unit's parent to that object
if (parent != null)
{
thisUnit.Parent = parent;
parent.Children.Add(thisUnit);
}
}
else
{
units.Add(thisUnit);
}
}
}
}
return units;
那是在填充的列表
http://oi41.tinypic.com/rmpe8n.jpg
0123的屏幕截圖那從單位表的SQL數據:
http://oi40.tinypic.com/mt12sh.jpg
問題:
其實填充的列表應該只有一個單元的對象不是11(指數0 - 10)。是的,列表中的第一個單元是正確填充的,但單元索引1 - 10不應該在列表中。
這是應該看起來像真正:
0
|--1
| |--3
| | |--9
| | |--10
| |--4
|--2
| |--5
| |--6
|--7
|--8
UPDATE和解決方案要做到這一點是使用對象關係映射器,如實體框架做
private static Unit FindParentUnit(List<Unit> units, int parentId)
{
Unit parent;
foreach (Unit u in units)
{
if (u.Id == parentId){
return u;
}
parent = FindParentUnit(u.Children, parentId);
if (parent != null)
return parent;
}
return null;
}
如何http://stackoverflow.com/questions/6037501/sql-data-hierarchy: 這裏,如果行是通過ID有序上升,只有工作的例子? – erikxiv 2012-02-08 07:56:18
查看[Wikipedia ON CTE](http://en.wikipedia.org/wiki/Common_table_expressions):*通用表表達式由DB2,Firebird [1],Microsoft SQL Server,Oracle,PostgreSQL,HyperSQL和H2(實驗)* – 2012-02-08 09:25:46
行 - 那有什麼問題?您的「根」人(ID = 0)有**四個孩子** - 正如您提供的數據所預期的那樣。我很確定,如果您深入瞭解根人員的「兒童」收藏,您也可以找到其他節點及其子女。 – 2012-02-16 06:00:05