数据库设计:
-- 无限分类 -- -- 数据库:DB_InfiniteCategory -- 数据表:Tb_Infinite --------------------------------------------------------------- -- 创建数据库 CREATE DATABASE DB_InfiniteCategory -- 创建数据表 USE DB_InfiniteCategory CREATE TABLE Tb_Infinite ( id int not null, --子级 pid int not null, --父级 categoryName varchar(10) not null --分类名称 ) --使用语句 select id, pid, categoryName from Tb_Infinite where pid = 0
代码:
1 using System; 2 using System.Web.UI; 3 using System.Data; 4 using System.Data.SqlClient; 5 6 using DAL; 7 using System.Web.UI.WebControls; 8 9 namespace InfiniteCategory 10 { 11 public partial class Default : System.Web.UI.Page 12 { 13 string toadd = "├"; 14 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 if (!Page.IsPostBack) 18 { 19 GetArticleCategory("0"); 20 } 21 } 22 23 public void GetArticleCategory(string pid) 24 { 25 SqlConnection conn = new SqlConnection(" server = HUANGFU-PC; database = DB_InfiniteCategory; integrated security = true"); 26 string sql = "select id,categoryName from Tb_Infinite where [email protected]"; 27 SqlCommand cmd = new SqlCommand(sql, conn); 28 SqlParameter Pid = new SqlParameter("@pid", SqlDbType.Int); 29 Pid.Value = pid; 30 cmd.Parameters.Add(Pid); 31 conn.Open(); 32 SqlDataReader sdr = cmd.ExecuteReader(); 33 while (sdr.Read()) 34 { 35 this.DropDownList1.Items.Add(new ListItem(toadd + " " + sdr[1].ToString(), sdr[0].ToString())); 36 toadd += "─┴"; 37 this.GetArticleCategory(sdr[0].ToString()); 38 toadd = toadd.Substring(0, toadd.Length - 2); 39 } 40 sdr.Close(); 41 conn.Close(); 42 } 43 } 44 }
最终效果:
时间: 2024-10-12 22:49:30