//把当前的数据单元写入 “数据单元表” public void WriteDataIntoDataUnitTbl() { if (ToTreeView.GetNodeCount(true) > 0) //有记录就更新 { GetDataTreeInitIndex dIndex = new GetDataTreeInitIndex(); string mypath = dIndex.GetDbInfo(); string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mypath + ";Mode=ReadWrite|Share Deny None;Persist Security Info=False"; //生成连接数据库字符串 OleDbConnection mycon = new OleDbConnection(constr); //定义OleDbConnection对象实例并连接数据库 string strExp = ""; strExp = "delete * from " + "数据单元表"; OleDbCommand aCommand = new OleDbCommand(strExp, mycon); try { mycon.Open(); //删除记录 int iRows = aCommand.ExecuteNonQuery(); //插入记录 SearchAllTreeNod(mycon, m_NewRootNode); //关闭连接,这很重要 mycon.Close(); } catch (System.Exception e) { Console.WriteLine(e.Message); } } }
public void InitToTree() { //从 数据单元表 中获取信息 GetDataTreeInitIndex dIndex = new GetDataTreeInitIndex(); string mypath = dIndex.GetDbInfo(); string strDispLevel = dIndex.GetXmlElementValue("UnitTree", "tIsDisp"); //是否从市级开始创建数据单元树 string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mypath + ";Mode=ReadWrite|Share Deny None;Persist Security Info=False"; //生成连接数据库字符串 OleDbConnection mycon = new OleDbConnection(constr); //定义OleDbConnection对象实例并连接数据库 string strExp = ""; strExp = "select * from 数据单元表"; OleDbCommand aCommand = new OleDbCommand(strExp, mycon); try { mycon.Open(); //创建datareader 对象来连接到表单 OleDbDataReader aReader = aCommand.ExecuteReader(); ToTreeView.Nodes.Clear(); TreeNode tparent; tparent = new TreeNode(); tparent.Text = "数据单元"; tparent.Tag = 0; tparent.ImageIndex = 0; tparent.SelectedImageIndex = 0; TreeNode tRoot; tRoot = new TreeNode(); tRoot = tparent; TreeNode tNewNode; TreeNode tNewNodeClild; TreeNode tNewLeafNode; ToTreeView.Nodes.Add(tparent); ToTreeView.ExpandAll(); while (aReader.Read()) { //如果是行政区级别是1就是根节点 //此处默认都已经排序合理,针对排序的维护在数据字典维护界面中实现 if (aReader["数据单元级别"].ToString().Equals("1")) //省级节点 { if (strDispLevel.Equals("0")) { tNewNode = new TreeNode(); tNewNode.Text = aReader["行政名称"].ToString(); tNewNode.Name = aReader["行政代码"].ToString(); tparent.Nodes.Add(tNewNode); tparent.Expand(); tparent = tNewNode; tNewNode.Tag = 1; tNewNode.ImageIndex = 1; tNewNode.SelectedImageIndex = 1; } } else if (aReader["数据单元级别"].ToString().Equals("2")) //市级节点 { tNewNodeClild = new TreeNode(); tNewNodeClild.Text = aReader["行政名称"].ToString(); tNewNodeClild.Name = aReader["行政代码"].ToString(); tparent.Nodes.Add(tNewNodeClild); tparent.Expand(); tRoot = tNewNodeClild; tNewNodeClild.Tag = 2; tNewNodeClild.ImageIndex = 1; tNewNodeClild.SelectedImageIndex = 1; } else if (aReader["数据单元级别"].ToString().Equals("3"))//县级节点 { tNewLeafNode = new TreeNode(); tNewLeafNode.Text = aReader["行政名称"].ToString(); tNewLeafNode.Name = aReader["行政代码"].ToString(); tRoot.Nodes.Add(tNewLeafNode); tNewLeafNode.Tag = 3; tNewLeafNode.ImageIndex = 1; tNewLeafNode.SelectedImageIndex = 1; } else { tNewNodeClild = new TreeNode(); tNewNodeClild.Text = aReader["行政名称"].ToString(); tNewNodeClild.Name = aReader["行政代码"].ToString(); tparent.Nodes.Add(tNewNodeClild); tparent.Expand(); tNewNodeClild.Tag = 1; tNewNodeClild.ImageIndex = 1; tNewNodeClild.SelectedImageIndex = 1; } } //关闭reader对象 aReader.Close(); //关闭连接,这很重要 mycon.Close(); } catch (System.Exception e) { Console.WriteLine(e.Message); } }