您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > TreeView控件与SQL数据库的应用(遍历算法)
开发环境:WinXPSP3,VS2008,SQL2000TreeView控件与SQL数据库的应用(遍历算法)(数据与TreeView的绑定及Treeview的增加、删除、修改、遍历等数据库的操作)这个是数据库连接及一些操作数据库的方法,文件:SqlManager.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Windows.Forms;namespaceTreeViewText{classSqlManager{SqlConnectionconn;stringstrConn;publicSqlManager(){strConn=@DataSource=.;InitialCatalog=TreeView;IntegratedSecurity=SSPI;PersistSecurityInfo=False;UserID=sa;Password=sa;conn=newSqlConnection(strConn);}publicDataSetGetDataSet(){DataSetds=newDataSet();SqlDataAdaptersda=newSqlDataAdapter(querySqlCmd,conn);try{conn.Open();sda.Fill(ds);}catch(Exceptionex){MessageBox.Show(ex.Message,获取数据失败!);}finally{if(conn.State==ConnectionState.Open){conn.Close();}}returnds;}publicDataSetGetDataSet(stringsqlCmd){DataSetds=newDataSet();SqlDataAdapterda=newSqlDataAdapter(sqlCmd,conn);try{conn.Open();da.Fill(ds);}catch(Exceptionex){MessageBox.Show(ex.Message,获取数据失败!);}finally{if(conn.State==ConnectionState.Open){conn.Close();}}returnds;}publicvoidRunSqlCommand(stringsqlCmd){SqlCommandcmd=newSqlCommand(sqlCmd,conn);try{conn.Open();cmd.ExecuteNonQuery();}catch(Exceptionex){MessageBox.Show(ex.Message,操作数据库失败!);}finally{if(conn.State==ConnectionState.Open){conn.Close();}}}privatestringm_querySqlCmd;#region获取SQL语句publicstringquerySqlCmd{get{if(m_querySqlCmd==null){returnselect*fromTreeNode;}returnquerySqlCmd;}set{m_querySqlCmd=value;}}#endregion}}主窗体:Form1.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceTreeViewText{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){//TreeNodetreeNode1=newTreeNode(节点1,newTreeNode[]{treeNode4,treeNode5});//treeView1.Nodes.AddRange(newTreeNode[]{treeNode1});}#regionTreeView节点编辑(没有修改数据库)privatevoidbtnEdit_Click(objectsender,EventArgse){if(treeView1.SelectedNode!=null)if(!treeView1.SelectedNode.IsEditing)treeView1.SelectedNode.BeginEdit();}privatevoidbtnDelete_Click(objectsender,EventArgse){if(treeView1.SelectedNode!=null)treeView1.SelectedNode.Remove();}privatevoidbtnAddNode_Click(objectsender,EventArgse){TreeNodetreeNodeTemp=newTreeNode(GetNodeText());if(treeView1.Nodes.Count0){if(treeView1.SelectedNode!=null){if(treeView1.SelectedNode.Parent!=null)treeView1.SelectedNode.Parent.Nodes.Add(treeNodeTemp);elsetreeView1.Nodes.Add(treeNodeTemp);}}else{treeView1.Nodes.Add(treeNodeTemp);}}privatevoidbtnAddChild_Click(objectsender,EventArgse){TreeNodetreeNodeTemp=newTreeNode(GetNodeText());if(treeView1.SelectedNode!=null){treeView1.SelectedNode.Nodes.Add(treeNodeTemp);}}#region获取文本框文本信息///summary///获取文本框文本信息////summary///returnsstring/returnsprivatestringGetNodeText(){if(!String.IsNullOrEmpty(tbNodeName.Text.Trim()))returntbNodeName.Text.Trim();returnTreeNode;}#endregion#endregion///summary///查询数据////summary///paramname=sender/param///paramname=e/paramprivatevoidbtnQuery_Click(objectsender,EventArgse){SqlManagersqlManager=newSqlManager();dataGridView1.DataSource=sqlManager.GetDataSet(sqlManager.querySqlCmd).Tables[0];//清空TreeView的内容treeView1.Nodes.Clear();//递归函数,0为根节点的父节点ParentID的值AddTreeView(0,(TreeNode)null);//非递归(两层分类显示)//ShowTreeView((TreeNode)null);//展开所有树节点//treeView1.Expand();}///summary///退出程序////summary///paramname=sender/param///paramname=e/paramprivatevoidbtnExit_Click(objectsender,EventArgse){this.Close();}#region获得数据库信息放入TreeView中(递归)privatevoidAddTreeView(intParentID,TreeNodeparentNode){SqlManagersqlManager=newSqlManager();DataViewdataTree=newDataView(sqlManager.GetDataSet().Tables[0]);stringfilter=ParentID=+ParentID;dataTree.RowFilter=filter;foreach(DataRowViewrowindataTree){TreeNodenode=newTreeNode();//处理根节点if(parentNode==null){node.Name=row[ID].ToString();node.Text=row[Name].ToString();treeView1.Nodes.Add(node);AddTreeView(Int32.Parse(row[ID].ToString().Trim()),node);}//处理子节点else{node.Name=row[ID].ToString();node.Text=row[Name].ToString();parentNode.Nodes.Add(node);AddTreeView(Int32.Parse(row[ID].ToString().Trim()),node);}}}#endregion#region获得数据库信息放入TreeView中(非递归函数,根据Category来分类,仅支持两层分类)/*privatevoidShowTreeView(TreeNodepNode){treeView1.Nodes.Clear();SqlManagersqlManager=newSqlManager();DataViewdataTree=newDataView(sqlManager.GetDataSet(selectdistinctCategoryfromTreeNode).Tables[0]);foreach(DataRowViewrowindataTree){TreeNodenode=newTreeNode();node.Text=row[Category].ToString();treeView1.Nodes.Add(node);CreateChildNodes(node);}}//获取第二层节点的的数据privatevoidCreateChildNodes(TreeNodetreenode){SqlManagersqlManager=newSqlManager();DataViewdataTree=newDataView(sqlManager.GetDataSet().Tables[0]);dataTree.RowFilter=Category='+treenode.Text+';foreach(DataRowViewrowindataTree){TreeNodenode=newTreeNode();node.Text=row[Name].ToString();treenode.Nodes.Add(node);}}*/#endregion///summary///添加记录////summary///paramname=sender/param///paramname=e
本文标题:TreeView控件与SQL数据库的应用(遍历算法)
链接地址:https://www.777doc.com/doc-3598120 .html