Below are the database tables
Drag a TreeView control in the design view
Default. aspx. cs Code
/*
* Created by: du Chenggang
* Creation Time: 22:05:30
* Note:
* All Rights Reserved: Xia chufeng duchenggang.accp@163.com.
*/
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Collections. Generic;
Using System. Data. SqlClient;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
List <FileInfo> listFileInfo = GetFileInfo ();
For (int I = 0; I <listFileInfo. Count; I ++)
{
TreeNode fatherNode = new TreeNode ();
If (listFileInfo [I]. ParentId = 0)
{
FatherNode. Value = listFileInfo [I]. FileId. ToString ();
FatherNode. Text = listFileInfo [I]. FileName. ToString ();
FatherNode = GetTreeNode (fatherNode, listFileInfo );
This. TreeView1.Nodes. Add (fatherNode );
}
}
}
}
Public static TreeNode GetTreeNode (TreeNode fatherNode, List <FileInfo> listFileInfo)
{
For (int I = 0; I <listFileInfo. Count; I ++)
{
TreeNode childNode = new TreeNode ();
If (listFileInfo [I]. ParentId. ToString () = fatherNode. Value)
{
ChildNode. Value = listFileInfo [I]. FileId. ToString ();
ChildNode. Text = listFileInfo [I]. FileName. ToString ();
ChildNode = GetTreeNode (childNode, listFileInfo );
FatherNode. ChildNodes. Add (childNode );
}
}
Return fatherNode;
}
Public static List <FileInfo> GetFileInfo ()
{
String SQL = "SELECT * FROM FileInfo ";
String connString = "Data Source =.; Initial Catalog = MyOffice; User Id = sa; PassWord = sa ;";
List <FileInfo> listFileInfo = new List <FileInfo> ();
SqlConnection con = new SqlConnection (connString );
Con. Open ();
SqlCommand cmd = new SqlCommand (SQL, con );
SqlDataReader reader = cmd. ExecuteReader ();
If (reader. HasRows)
{
While (reader. Read ())
{
FileInfo fileInfo = new FileInfo ();
FileInfo. FileId = (int) reader ["FileId"];
FileInfo. FileName = (string) reader ["FileName"];
FileInfo. FileType = (int) reader ["FileType"];
FileInfo. Remark = (string) reader ["Remark"];
FileInfo. FileOwner = (string) reader ["FileOwner"];
FileInfo. CreateDate = (DateTime) reader ["CreateDate"];
FileInfo. ParentId = (int) reader ["ParentId"];
FileInfo. FilePath = (string) reader ["FilePath"];
FileInfo. IfDelete = (int) reader ["IfDelete"];
ListFileInfo. Add (fileInfo );
}
Reader. Close ();
}
Con. Close ();
Return listFileInfo;
}
}
FileInfo. cs entity class ....
/*
* Created by: du Chenggang
* Creation Time: 22:05:30
* Note:
* All Rights Reserved: Xia chufeng duchenggang.accp@163.com.
*/
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
/// <Summary>
/// Summary of FileInfo
/// </Summary>
Public class FileInfo
{
Private int fileId;
Private string fileName = String. Empty;
Private int fileType;
Private string remark = String. Empty;
Private string fileOwner = String. Empty;
Private DateTime createDate;
Private int parentId;
Private string filePath = String. Empty;
Private int ifDelete;
Public FileInfo (){}
Public int FileId
{
Get {return this. fileId ;}
Set {this. fileId = value ;}
}
Public string FileName
{
Get {return this. fileName ;}
Set {this. fileName = value ;}
}
Public int FileType
{
Get {return this. fileType ;}
Set {this. fileType = value ;}
}
Public string Remark
{
Get {return this. remark ;}
Set {this. remark = value ;}
}
Public string FileOwner
{
Get {return this. fileOwner ;}
Set {this. fileOwner = value ;}
}
Public DateTime CreateDate
{
Get {return this. createDate ;}
Set {this. createDate = value ;}
}
Public int ParentId
{
Get {return this. parentId ;}
Set {this. parentId = value ;}
}
Public string FilePath
{
Get {return this. filePath ;}
Set {this. filePath = value ;}
}
Public int IfDelete
{
Get {return this. ifDelete ;}
Set {this. ifDelete = value ;}
}
}