界面如下:
接着是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 资料管理器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
//加载文章到treeview
LoadCategoryToTree();
}
private void LoadCategoryToTree()
{
string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select tId,tName from Category where tParentId=-1";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
int categoryId = reader.GetInt32(0);
string categoryName = reader.GetString(1);
//将当前类别加载到Treeview上
TreeNode tnode = treeView1.Nodes.Add(categoryName);
tnode.Tag = categoryId;
//加载当前类别categoryId下的所有子类别
//select tId,tName from Category where tParentId=categoryId
//将当前类别categoryId下的所有的子类别。加载到tnode节点下
LoadSubCategory(categoryId, tnode);
}
}
}
}
}
}
private void LoadSubCategory(int categoryId, TreeNode tnode)
{
string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string sql = "select tId,tName from Category where tParentId=@pid";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
cmd.Parameters.AddWithValue("@pid", categoryId);
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
int cId = reader.GetInt32(0);
string categoryName = reader.GetString(1);
TreeNode subTNode = tnode.Nodes.Add(categoryName);
subTNode.Tag = cId;
}
}
}
}
}
}
//选中类别改变后的事件
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//当选中的是二级节点的时候level==1
if (e.Node.Level==1)
{
// e.Node表示当前选中的节点
object categoryId = e.Node.Tag;
//根据类别ID(categoryId)加载该类别下的所有文章到listbox
LoadContentInfo(categoryId);
}
}
private void LoadContentInfo(object categoryId)
{
listBox1.Items.Clear();
textBox1.Clear();
string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection con=new SqlConnection(constr))
{
string sql = "select did,dname from ContentInfo where dtid=@cateId";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
cmd.Parameters.AddWithValue("@cateId", categoryId);
con.Open();
using (SqlDataReader reader=cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
int did = reader.GetInt32(0);
string dname = reader.GetString(1);
Essay wz=new Essay();
wz.WenZhangId=did;
wz.WenZhangBiaoti=dname;
listBox1.Items.Add(wz);
}
}
}
}
}
}
//文章标题选择项改变事件
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem!=null)
{
Essay wz = (Essay)listBox1.SelectedItem;
int did = wz.WenZhangId;
string msg= GetWenZhangNeiRongById(did);
textBox1.Text = msg;
}
}
private string GetWenZhangNeiRongById(int did)
{
string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (SqlConnection con=new SqlConnection(constr))
{
string sql = "select dcontent from ContentInfo where dId=@id";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
cmd.Parameters.AddWithValue("@id",did);
con.Open();
object obj = cmd.ExecuteScalar();
return obj.ToString();
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
还有一个数据类:Essay类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 资料管理器
{
public class Essay
{
public int WenZhangId
{
get;
set;
}
public string WenZhangBiaoti
{
get;
set;
}
public override string ToString()
{
return WenZhangBiaoti;
}
}
}