winform 资料管理器

界面如下:

接着是代码:

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;
      }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值