2011-11-22 1 views
1

값 목록을 가져 오려면 한 값을 얻은 후에 값을 전달하고 같은 방법을 호출하십시오.C에서 첫 번째 값이 누락 된 순환 루프

코드가 잘 작동하고 내가 60

을 통과하지만, 때 모든 값을 반환

60 
| 
61 
| 
63 
| 
64 

같은 구조 인 경우 구조는 여기에 너무

60 
    | | 
    61 63 
    | 
    62 

같은 경우 61 및 63의 상위 ID는 60입니다.

그러면 첫 번째 ex 61과 63은 동시에 읽히고 63은 마지막 읽기 orgid이므로 63을 다음 루프로 전달하고 0을 반환하고 루프 끝을 반환하므로 ecution은 63입니다. 따라서 orgid 61을 사용하여 실행이 수행되지 않으므로 62가 누락되었습니다. 내가 여기서 실수하고있는 것은 무엇인가. 독자가 동일한 쿼리에서 2 개의 값을 읽으면 두 번째 값이 전달됩니다. 쿼리에서 읽은 모든 값을 전달하고 싶습니다.

private DataSet BindGridView(int id, List<int> userids) 
{ 
    string GetOrgIDs = "select OrganisationID from tbl_organisation where ParentID [email protected]"; 
    MySqlParameter[] paramet = new MySqlParameter[1]; 
    paramet[0] = new MySqlParameter("@ParentID", MySqlDbType.Int32); 
    paramet[0].Value = id; 
    int Orgid = 0; 
    MySqlDataReader reader = server.ExecuteReader(CommandType.Text, GetOrgIDs, paramet); 
    while (reader.Read()) 
    { 
     Orgid = Convert.ToInt32(reader["OrganisationID"]); 
     userids.Add(Orgid); 
    } 
    reader.Close(); 
    if (Orgid != 0) 
    { 
     BindGridView(Orgid, userids); 
    } 
} 

은 내가,

답변

2

발견 된 마지막 ID의 부모로 재귀합니다. 따라서 이전 값의 가능한 하위를 무시합니다.

발견 된 값을 목록 매개 변수와 (새) 로컬 목록에 모두 추가하십시오. 그런 다음 해당 로컬 목록을 반복하고 값이! = 0이면 되풀이합니다.

이렇게하면 여러 결과 집합이 열리지 않습니다.

private DataSet BindGridView(int id, List<int> userids) 
{ 
    string GetOrgIDs = "select OrganisationID from tbl_organisation where ParentID [email protected]"; 
    MySqlParameter[] paramet = new MySqlParameter[1]; 
    paramet[0] = new MySqlParameter("@ParentID", MySqlDbType.Int32); 
    paramet[0].Value = id; 
    List<int> children = new List<int>(); // new local list 
    MySqlDataReader reader = server.ExecuteReader(CommandType.Text, GetOrgIDs, paramet); 
    while (reader.Read()) 
    { 
     int orgid = Convert.ToInt32(reader["OrganisationID"]); 
     userids.Add(orgid); 
     children.Add(orgid); // also add to local list 
    } 
    reader.Close(); 

    foreach(int child in children) 
    if (child != 0) 
    { 
     BindGridView(child, userids); 
    } 
} 
+0

몇 가지 코드로 나를 보여줄 수 있습니까? 감사합니다 – Mark

+0

나는 이미 그것을 추가하고 있었다 :-) –

4

귀하의 재귀 호출이 각각 Orgid를 호출하기 위해 while 루프에 앉아해야합니다 그러나이의 MySQL DB를 사용하고

List<int> OrgIDs = new List<int>(); 
       OrgIDs.Add(60); 
       BindGridView(60, OrgIDs); 

페이지로드에서 메소드를 호출 여러 개의 동시 읽기가 데이터베이스에 자원에 연결되는 결과가 될 수 있습니다.

while (reader.Read()) 
{ 
    Orgid = Convert.ToInt32(reader["OrganisationID"]); 
    userids.Add(Orgid); 
    if (Orgid != 0) 
    { 
     BindGridView(Orgid, userids); 
    } 
} 
reader.Close(); 
+0

여기에 60이 전달되면 먼저 61을 얻고 61을 메서드에 전달하면 62가됩니다. 그러나 63을 놓치지 않을 것이고 또한 얻을 것입니다. 이미이 Connection과 연결된 열려있는 DataReader가 있습니다. 먼저 닫아야합니다. 오류 – Mark

0

이 루프는 100x 번 실행할 수 있습니다.

while (reader.Read()) 
{ 
    Orgid = Convert.ToInt32(reader["OrganisationID"]); 
    userids.Add(Orgid); 
} 
reader.Close(); 

모든 레코드가 반복 된 후에는 마지막 값만 있습니다.

if (Orgid != 0) 
{ 
    BindGridView(Orgid, userids); 
} 

BindGridView (Orgid, userids); reader.read() 루프의 마지막 OrgId에서만 호출됩니다.

나는 (데이터 순서에 따라) 63에서 BindGridView를 호출하고 61과 다른 것을 건너 뛸 것이라고 생각한다.

+0

네, 그게 내가 풀고 싶은 문제입니다. – Mark

+0

죄송합니다, 나는 몇 가지 예제 코드를 수정해야합니다. 앤디와 한스의 다른 좋은 예가 이미 있습니다. – user1231231412