OracleConnection conn = new OracleConnection(connString);
try
{
//连接数据库
conn.Open();
//查询数据库
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "select no,name from sys.t_org where type=1 ";
OracleDataReader dr=cmd.ExecuteReader();
if (dr.HasRows)
{
while(dr.Read()){
string no = dr["no"].ToString();
string name = dr["name"].ToString();
TreeNode node = new TreeNode();
node.Name = no;
node.Text = name;
directoryTree.Nodes.Add(node);
cTree(node,node.Name);
}
}
// MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
conn.Close();
}
递归方法:
string connString = "User ID=sys;Password=sys;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.11)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))";
OracleConnection conn = new OracleConnection(connString);
try
{
//连接数据库
conn.Open();
//查询数据库
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "select no,name from sys.t_org where p_no='"+node.Name+"'";
OracleDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string no = dr["no"].ToString();
string name = dr["name"].ToString();
TreeNode nodes = new TreeNode();
nodes.Name = no;
nodes.Text = name;
node.Nodes.Add(nodes);
cTree(nodes,nodes.Name);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
conn.Close();
}
效果图: