基于数据库的资源管理器简单版
来源:互联网 发布:英语翻译网站 知乎 编辑:程序博客网 时间: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;
}
}
}
}
}
还有其余几个简单类,可自行写出,这里就不做赘述了。。。
- 基于数据库的资源管理器简单版
- Unity3d简单的资源管理器
- 简单资源管理器
- 自己写的一个简单的资源管理器
- 基于策略的泛型资源管理器-初步思考
- 简单仿资源管理器(递归)
- 简单中级项目Servlet/JSP实现web版windows资源管理器
- 基于数据库的CS程序的简单安全体系
- 基于c语言的简单的mysql数据库操作
- 基于sqlite3数据库的C、C++开发-简单
- 基于json数据格式实现的简单数据库——jsonDB
- C语言编写基于mysql数据库的简单数据管理系统
- Java 基于MySQL数据库的简单学生管理系统
- 基于sqlite3的利用数据库实现简单通讯录
- javaweb-基于数据库的简单登录注册界面
- MyEclipse数据库资源管理器首选项
- 建立自己的资源管理器
- 资源管理器的启动参数
- POJ 3630 Phone List 【判断串是否是其他串的前缀】
- 设计模式——适配器模式(C++)
- 清晰解题:扔鸡蛋问题
- equals与==的区别
- PNG格式小图标的CSS任意颜色赋色技术
- 基于数据库的资源管理器简单版
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
- java RS232串口通讯(电子天平数据抓取)
- 基本算法-partition函数
- iOS应用安全读书笔记之通话记录
- 网络体系结构以及路由器在当中扮演的角色
- Android 消息机制--handler
- CentOS 编译安装 MySQL5.7
- 转发与重定向的区别