public MyTreeNode(string sysId, string topId) { this.SysId = sysId; //如果sysId = topId,为避免误读为闭环,将topId设置为空 if (sysId == topId) { this.TopId = ""; } else { this.TopId = topId; } this.Level = 0; switch (TextUtil.enUpperLower) { case EnumUpperLower.Lower: SysId = SysId.ToLower(); TopId = TopId.ToLower(); break; case EnumUpperLower.Upper: SysId = SysId.ToUpper(); TopId = TopId.ToUpper(); break; default: break; } switch (TextUtil.enTrim) { case EnumTrim.All: SysId = SysId.Trim(); TopId = TopId.Trim(); break; case EnumTrim.Start: SysId = SysId.TrimStart(); TopId = TopId.TrimStart(); break; case EnumTrim.End: SysId = SysId.TrimEnd(); TopId = TopId.TrimEnd(); break; default: break; } switch (TextUtil.enDBCSBC) { case EnumDBCSBC.DBC: SysId = TextUtil.SBCToDBC(SysId); TopId = TextUtil.SBCToDBC(TopId); break; case EnumDBCSBC.SBC: SysId = TextUtil.DBCToSBC(SysId); TopId = TextUtil.DBCToSBC(TopId); break; default: break; } }
//读取文件数据并构造树节点 private static bool ReadLine2Node(string filepath, string separator, bool confirm) { Encoding encoding = TextUtil.GetFileEncodeType(filepath); StreamReader mysr = new StreamReader(filepath, encoding); int row = 0; try { WindowAdmin.notify.SetProcessBarVisible(true); WindowAdmin.notify.SetStatusMessage("开始读取文件......"); string firstLine = mysr.ReadLine(); //第一行是表头,读取之后不处理,直接跳过 if (!DBUtil.CheckHead(firstLine, separator, confirm)) //检查表头 { string errMsg = "文件格式不正确,最少必须包含三列,前三列为“会员id,上级会员id,会员姓名”且顺序固定,请重新选择正确的文件!"; MessageBox.Show(errMsg); WindowAdmin.notify.SetStatusMessage(errMsg); mysr.Close(); return(false); } while (!mysr.EndOfStream) { string line = mysr.ReadLine(); string[] aryline = line.Split(new String[] { separator }, StringSplitOptions.None); if (!DBUtil.CheckLen(aryline)) { //发现出错的数据 string errMsg = "该csv数据文件中包含错误数据,请先对csv文件进行检查校准!"; MessageBox.Show(errMsg); WindowAdmin.notify.SetStatusMessage(errMsg); mysr.Close(); return(false); } MyTreeNode myNode = new MyTreeNode(aryline[0], aryline[1]); if (allNodes.ContainsKey(myNode.SysId)) //ID有重复的节点 { IdConflictNodes.Add(myNode); } else { allNodes.Add(myNode.SysId, myNode); } row++; if (row % 1000 == 0) { WindowAdmin.notify.SetStatusMessage("【第一步:正在读取第" + row + "个节点】——>【第二步:构造树结构】——>【第三步:写入数据库】"); } } allNodeCount = row; } catch (Exception ex) { WindowAdmin.notify.SetProcessBarVisible(false); WindowAdmin.notify.SetStatusMessage("文件读取出错!+\n" + ex.Message); mysr.Close(); return(false); } mysr.Close(); return(true); }