using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO; namespace 资料管理器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//加载类别到TreeView
LoadCategoryToTree(treeView1.Nodes, GetCategoriesByParentId(-)); //设置TreeView中节点单击的时候显示右键菜单 AddMouseClickShowContentMenu();
} private void AddMouseClickShowContentMenu()
{
//遍历TreeView的每个节点
SearchNode(treeView1.Nodes);
} private void SearchNode(TreeNodeCollection treeNodeCollection)
{
foreach (TreeNode item in treeNodeCollection)
{
if (item.Level == )
{
item.ContextMenuStrip = contextMenuStrip1;
}
SearchNode(item.Nodes);
}
} //递归加载类别信息到TreeView
private void LoadCategoryToTree(TreeNodeCollection treeNodeCollection, List<Category> list)
{
foreach (Category item in list)
{
//把当前节点加到treeNodeCollection集合中
TreeNode tnode = treeNodeCollection.Add(item.TName); //把当前类别的Id记录到Tag中。
tnode.Tag = item.TId;
//List<Category> listSub = GetCategoriesByParentId(item.TId);
//if (listSub.Count == 0)
//{
// tnode.ContextMenuStrip = contextMenuStrip1;
//}
LoadCategoryToTree(tnode.Nodes, GetCategoriesByParentId(item.TId));
}
} private List<Category> GetCategoriesByParentId(int pid)
{
List<Category> list = new List<Category>();
string sql = "select tid,tname from Category where tParentId=@pid";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@pid", pid)))
{
if (reader.HasRows)
{
while (reader.Read())
{
Category model = new Category();
model.TId = reader.GetInt32();
model.TName = reader.GetString();
list.Add(model);
}
}
}
return list;
} private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node != null)
{
LoadTitleToListBox(e.Node); }
} private void LoadTitleToListBox(TreeNode node)
{ //获取当前节点对应的类别id
int categoryId = (int)node.Tag;
//从文章表中查询dtid为categoryId的所有文章
List<ContentInfo> list = GetContentByCategoryId(categoryId);
listBox1.Items.Clear();
//遍历list集合将文章信息加到Listbox中
foreach (var item in list)
{
listBox1.Items.Add(item);
}
} private List<ContentInfo> GetContentByCategoryId(int categoryId)
{
List<ContentInfo> list = new List<ContentInfo>();
//select did,dname from ContentInfo where dtid=@categoryId
using (SqlDataReader reader = SqlHelper.ExecuteReader("select did,dname from ContentInfo where dtid=@categoryId", new SqlParameter("@categoryId", categoryId)))
{
if (reader.HasRows)
{
while (reader.Read())
{
ContentInfo model = new ContentInfo();
model.DId = reader.GetInt32();
model.DName = reader.GetString();
list.Add(model);
}
}
} return list; } private void listBox1_MouseClick(object sender, MouseEventArgs e)
{ } private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
//ListBox的鼠标双击事件
// e.
if (listBox1.SelectedItem != null)
{
//获取选中项中的文章的Id
ContentInfo article = listBox1.SelectedItem as ContentInfo;
int id = article.DId;
textBox1.Text = GetContentInfoByContentId(id);
}
} //根据文章Id获取文章内容
private string GetContentInfoByContentId(int id)
{
//string sql = "select dcontent from ContentInfo where did=@id";
//using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@id", id)))
//{
// if (reader.HasRows)
// {
// if (reader.Read())
// {
// return reader.GetString(0);
// }
// }
//}
//return string.Empty;
string sql = "select dcontent from ContentInfo where did=@id";
return SqlHelper.ExecuteScalar(sql, new SqlParameter("@id", id)).ToString();
} private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
treeView1.SelectedNode = e.Node;
} private void 导入文章ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
//获取当前选中类别的categoryId
int categoryId = (int)treeView1.SelectedNode.Tag;
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.FileName = string.Empty;
//导入文章
//1.弹出一个选择路径的一个对话框
DialogResult result = openFileDialog1.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK)
{
//获取用户选择的文件的路径
string path = openFileDialog1.FileName;
string title = Path.GetFileNameWithoutExtension(path);
string content = File.ReadAllText(path, System.Text.Encoding.Default);
//执行insert语句将该文章导入到数据库中
string sql = "insert into ContentInfo(dtid,dname,dcontent) values(@categoryId,@title,@content)";
SqlParameter[] pms = new SqlParameter[] {
new SqlParameter("@categoryId",categoryId),
new SqlParameter("@title",title),
new SqlParameter("@content",content)
};
//直接执行插入语句
SqlHelper.ExecuteNonQuery(sql, pms);
} //重新加载ListBox
LoadTitleToListBox(treeView1.SelectedNode);
} }
}
}

使用TreeView+ListBox+TxtBox 资料管理器的更多相关文章

  1. 第20课-数据库开发及ado.net 可空值类型,资料管理器,多条件查询,Case

    第20课-数据库开发及ado.net 可空值类型,资料管理器,多条件查询,Case SqlHelper using System; using System.Collections.Generic; ...

  2. 【基础巩固】文件流读写、大文件移动 FileStream StreamWriter File Path Directory/ ,m资料管理器(递归)

    C#获取文件名 扩展名 string fullPath = @"d:\test\default.avi"; string filename = Path.GetFileName(f ...

  3. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备三)

    接下来,我们将继续分析UIlib.h文件中其他的文件, UIContainer.h, UIRender.h, WinImplBase.h, UIManager.h,以及其他布局.控件等: 1. UIR ...

  4. 5、Java Swing布局管理器(FlowLayout、BorderLayout、CardLayout、BoxLayout、GirdBagLayout 和 GirdLayout)

    5.Java-Swing常用布局管理器       应用布局管理器都属于相对布局,各组件位置可随界面大小而相应改变,不变的只是其相对位置,布局管理器比较难以控制,一般只在界面大小需要改是才用,但即使这 ...

  5. 工控随笔_08_西门子_Win10安装Step7.V5.6中文版授权管理器不能正常启动

    随着Windows系统的不断升级,西门子工控软件也不断升级,但是有时候在安装西门子 软件的时候会出现授权管理器不能正常启动的情况. 图  Step7 因为自动许可证管理器不能正常打开 如上图所示,报S ...

  6. Clipboard Action for Mac(智能剪贴板历史管理器)破解版安装

    1.软件简介    Clipboard Action 是 macOS 系统上一款智能剪贴板历史管理器,它允许剪贴板历史中的每一段内容执行操作.使用 AppleScript 或 Automator 工作 ...

  7. Java-Swing常用布局管理器

    http://www.cnblogs.com/hthuang/p/3460234.html   5.Java-Swing常用布局管理器       应用布局管理器都属于相对布局,各组件位置可随界面大小 ...

  8. ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

    在用c#生成应用程序的时候,读写dbf时,open方法出错 ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 以前这个程序是用着好 ...

  9. Atitit.木马病毒的操作注册表原理 系统服务管理器 atiSysService

    Atitit.木马病毒的操作注册表原理 系统服务管理器 atiSysService 1. atiSysService1 2. atiSysService  原理1 3. Java code1 4. 参 ...

随机推荐

  1. MVC神韵---你想在哪解脱!(十四)

    修正票价字段的精度 前面我们追加数据的时候遗留下来一个问题,就是在追加数据的时候,票价(Price)字段中输入的是9.99元,但是电影清单显示画面中该数据的票价字段显示为10元,这是为什么?这个问题发 ...

  2. Oracle数据库的版本变迁功能对比

    Oracle数据库自发布至今,也经历了一个从不稳定到稳定,从功能简单至强大的过程.从第二版开始,Oracle的每一次版本变迁,都具有里程碑意义. 1979年的夏季,RSI(Oracle公司的前身,Re ...

  3. getDefinitionByName与ApplicationDomain.getDefinition

    主swf 定义:MC,被加载swf 定义:MC.MC1 ①父SWF的应用程序域的新建子域 (默认方式)称其为应用程序域的继承 var app:ApplicationDomain=new Applica ...

  4. C#中使用GUID的笔记

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...

  5. w3cmark前端精彩博文周报 10.20-10.27

    w3cmark 官方Q群 145423956 | 官方微博 @w3cmark 自从最近微博屏蔽了我的站点域名,就很懒了.毕竟和不爽,一个纯技术站点还被认为不安全链接,还申诉无门,那些所谓的客服都是自动 ...

  6. password安全之动态盐

    首先,我们看看什么是盐:http://zh.wikipedia.org/zh/%E7%9B%90_%28%E5%AF%86%E7%A0%81%E5%AD%A6%29 ,再MD5是d16e970d6e5 ...

  7. iOS开发——UI篇OC篇&初始化图片方式

    初始化图片方式 一.读取图片 1.从资源(resource)读取 [cpp] view plaincopyprint?   UIImage* image=[UIImage imageNamed:@&q ...

  8. JavaScript eval() Function

    Evaluate/Execute JavaScript code/expressions: Example var x = 10;var y = 20;var a = eval("x * y ...

  9. QQWry.dat 数据写入

    纯真IP库 数据多,更新及时,很多同学在用,网上关于其读取的帖子也有不少(当然其中有一些是有BUG的),但却很少有关于其写入的帖子.OK,下面分享下写QQWry.dat. QQWry.dat 分三个部 ...

  10. php生成excel或php生成csv

    一.php生成excel 使用phpexcel类文件生成 二.php生成csv <?php$action ="make";if ($action=='make'){ $fp ...