基于数据库的资源管理器简单版

来源:互联网 发布:英语翻译网站 知乎 编辑:程序博客网 时间:2024/05/01 15:33

说明:

   用ADO.NET技术,将数据库连接起来,用winform的形式,将其功能展现出来。

  自学代码,有不足指出请多多指教。。。。

  有想法也可评论 

一起学习,一起探讨ha



下面代码:

winform控件的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace 资料管理器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 加载主级目录,并调用递归方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            List<Category> list = GetCatagoryBytfrientId(-1);//在数据库里将主级目录读出
            LoadGategory(list, tv.Nodes);
        }
        /// <summary>
        /// 递归方法,将集合加载到控件上
        /// </summary>
        /// <param name="list"></param>
        /// <param name="nodes"></param>
        private void LoadGategory(List<Category> list, TreeNodeCollection nodes)
        {
            foreach (Category item in list)
            {
                TreeNode tn= nodes.Add(item.TName);
                tn.Tag = item.TId;
                //根据数据库表的内容分析出要这样调用方法
                //递归方法
                LoadGategory( GetCatagoryBytfrientId(item.TId),tn.Nodes);


            }
        }
        /// <summary>
        /// 将数据库的数据加载到集合里面
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        private List<Category> GetCatagoryBytfrientId(int v)
        {
            List<Category> list = new List<Category>();
            string sql = "SELECT tID,TName FROM category WHERE tparentid=" + v;
            SqlDataReader reader= SQLHelper.ExcuteReader(sql);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Category category = new Category();
                    category.TId = Convert.ToInt32(reader["tid"]);
                    category.TName = reader["tName"].ToString();
                    list.Add(category);
                }
            }
            return list;
        }
        /// <summary>
        /// 双击事件,点击后显示三级内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //错误判别方法
            //if (tv.SelectedNode.NextNode!=null)
            //{
            //    listBox1.DataSource = GetContntInfo(Convert.ToInt32(tv.SelectedNode.Tag));
            //    listBox1.DisplayMember = "DNAme";


            //}
            listBox1.DataSource = GetContntInfo(Convert.ToInt32(tv.SelectedNode.Tag));
            listBox1.DisplayMember = "DNAme";
        }
        /// <summary>
        /// 将数据库的苏剧加载到集合里面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<ContentInfo> GetContntInfo(int id)
        {
            List<ContentInfo> list = new List<ContentInfo>();
            string sql = "SELECT did,dname FROM contentinfo WHERE dtid=" + id;
            SqlDataReader reader= SQLHelper.ExcuteReader(sql);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    ContentInfo coninfo = new ContentInfo();
                    coninfo.DId = Convert.ToInt32(reader["did"]);
                    coninfo.DName = reader["dNAme"].ToString();
                    list.Add(coninfo);
                }
            }
            return list;
        }
        /// <summary>
        /// 选择事件,将选定的内容显示到文本框控件中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //将选定的内容转换为所需类型
            ContentInfo con=  listBox1.SelectedItem as ContentInfo;
            textBox1.Text = GetContentInfoText(con.DId);
        }
        /// <summary>
        /// 返回首首行首列,即将内容返回
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private string GetContentInfoText(int id)
        {
            string sql = "SELECT dcontent FROM Contentinfo WHERE did=" + id;
            return SQLHelper.ExcuteScalar(sql).ToString();
        }
    }
}



SQLHelp类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
namespace 资料管理器
{
    public class SQLHelper
    {
        static readonly string str = ConfigurationManager.ConnectionStrings["strSQL"].ConnectionString;
        public static int ExcuteNonQuary(string sql,params SqlParameter[]param)
        {
            using (SqlConnection con=new SqlConnection(str))
            {
                using (SqlCommand com=new SqlCommand(sql,con))
                {
                    con.Open();
                    com.Parameters.AddRange(param);
                    return com.ExecuteNonQuery();
                }
            }
        }
        public static object ExcuteScalar(string sql, params SqlParameter[] param)
        {
            using (SqlConnection con = new SqlConnection(str))
            {
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                    con.Open();
                    com.Parameters.AddRange(param);
                    return com.ExecuteScalar();
                }
            }
        }
        public static SqlDataReader ExcuteReader(string sql, params SqlParameter[] param)
        {
            SqlConnection con = new SqlConnection(str);
            using (SqlCommand com=new SqlCommand(sql,con))
            {
                try
                {
                    con.Open();
                    return com.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
                catch (Exception ex)
                {
                    con.Close();
                    con.Dispose();
                    throw ex;
                }
            }
        }
    }
}


还有其余几个简单类,可自行写出,这里就不做赘述了。。。

0 0
原创粉丝点击