用c#做的
public List list = new List();//把所有的id都装入list中
SqlConnection con=null;
private void button1_Click(object sender, EventArgs e)//测试方法
{
GetIdsByName("软件");
}
//根据name获取ID public void GetIdsByName(String name)
{
con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=BBS;Integrated Security=True");
String sql = "select boardID from Board where boardName='"+name+"'";//根据A查询得到ID
con.Open();
SqlCommand cmd = new SqlCommand(sql ,con);
int id=Convert.ToInt32(cmd.ExecuteScalar());
getIDs(id);//去查它的子ids
con.Close();
}
public void getIDs(int id)//根据一个id查找它下面所有的子ID
{
String childsql = "select boardID from Board where parentId=" + id;// 根据父id查出它的所有子ID
SqlCommand ccmd = new SqlCommand(childsql, con);
SqlDataReader reader = ccmd.ExecuteReader();
List ids = new List();
while(reader.Read())
{
ids.Add(Convert.ToInt32(reader[0]));//把ids放入集合java中不用,C#中为了下面遍历用
list.Add(Convert.ToInt32(reader[0]));//添加到指定集合中
// getIDs(Convert.ToInt32(reader[0]));//在java里可以直接调用
}
reader.Close();
//在java里就不用了
foreach (int cid in ids)
{
getIDs(cid);//递归调用 }
}