/// <summary>
        /// 将普通英文与UTF8混合字串转换成正常字串
        /// </summary>
        /// <param name="混合字符串">英文与UTF8混合字串</param>
        /// <returns></returns>
        private string Hex2ChiEngString(string 混合字符串)
        {
            int           处理结束所在Index = 0;
            int           中文字开始Index, 中文字结束Index;
            StringBuilder result = new StringBuilder(); // 真实字符串

            // 每次循环优先处理英文,然后在处理中文。以此类推
            while (处理结束所在Index < 混合字符串.Length)
            {
                中文字开始Index = 混合字符串.IndexOf("&#x", 处理结束所在Index);
                if (中文字开始Index == -1)          // 如果接下来没有UTF8字符了
                {
                    中文字开始Index = 混合字符串.Length; // 使开始Index出界
                }
                if (中文字开始Index != 处理结束所在Index) // 有不用处理的英文字母直接放入原文
                {
                    result.Append(混合字符串.Substring(处理结束所在Index, 中文字开始Index - 处理结束所在Index));
                    处理结束所在Index = 中文字开始Index;
                }
                if (处理结束所在Index < 混合字符串.Length)                      // 还没处理完所有的字符串
                {
                    中文字结束Index = 混合字符串.IndexOf(";", 中文字开始Index) + 1; // 字符格式是&#x…;
                    // 循环查找连续的中文字符
                    while (true)
                    {
                        // 如果不再是中文字符,就跳出循环
                        if (混合字符串.IndexOf("&#x", 中文字结束Index) != 中文字结束Index)
                        {
                            break;
                        }
                        // 下个字还是中文就接着查找
                        中文字结束Index = 混合字符串.IndexOf(";", 中文字结束Index) + 1;
                    }
                    if (中文字结束Index == 0)
                    {
                        throw new Exception("中文Hex格式错误");
                    }
                    // 得到所有中文字符的utf8编码
                    string chinCharacterUtf8编码 = 混合字符串.Substring(中文字开始Index, 中文字结束Index - 中文字开始Index).Replace("&#x", "").Replace(";", "");
                    // 转化为汉字添加进去
                    result.Append(UnicodeSaverUtil.GetChsFromHex(chinCharacterUtf8编码));
                    处理结束所在Index = 中文字结束Index;
                }
            }
            return(result.ToString());
        }
        private string Text2Hex(string oldContent)
        {
            string formatedContent = ""; //只对oldContent中的中文部分进行utf8编码,其他部分不予处理

            foreach (char ch in oldContent.ToCharArray())
            {
                if (UnicodeSaverUtil.IsChineseChar(ch) || ch == '\'') //对所有中文或者'进行编码
                {
                    formatedContent += UnicodeSaverUtil.GetHexFromChs(ch);
                }
                else
                {
                    formatedContent += ch;
                }
            }
            return(formatedContent);
        }
        private void RefreshRemarks()
        {
            DataTable result = SqlUtil.Select("[Remark],[Table]",
                                              "Id 序号,Remark 注释,开机时间 对应开机时间,RemarkTime 注释创建时间",
                                              "[Table].序号 = [Remark].Id");

            for (int rowIndex = 0; rowIndex < result.Rows.Count; rowIndex++)
            {
                DataRow r = result.Rows[rowIndex];
                for (int columnIndex = 0; columnIndex < r.ItemArray.Count(); columnIndex++)
                {
                    string itemString = r.ItemArray[columnIndex].ToString();
                    if (UnicodeSaverUtil.IsChineseString(itemString))
                    {
                        string transformResult = Hex2ChiEngString(itemString);
                        r[columnIndex] = transformResult;
                    }
                }
            }
            this.tabControl1.SelectedIndex      = 0;
            this.dataGridViewRemarks.DataSource = result;
            this.dataGridViewRemarks.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
        }
Пример #4
0
        /// <summary>
        /// 将显示的一条转换成可以直接执行的sql字符串
        /// </summary>
        /// <param name="displayedItem">显示在屏幕上的单行文字</param>
        /// <returns></returns>
        private SqlItem GetSqlItemFromDisplayedString(string displayedItem)
        {
            // 直观模式的每条格式
            // 开机时间:2019-12-26 21:31:16.410
            // 关机时间: -- 2019-12-26 21:31:16.410
            if (opState == OutputState.MODERN)
            {
                if (UnicodeSaverUtil.IsChineseString(displayedItem))
                {
                    throw new ArgumentException("直观模式的时间字符串存在非法字符!");
                }

                if (displayedItem.Contains(" -- ") == false) //开机
                {
                    string  time      = displayedItem;
                    string  sqlString = "INSERT INTO [Table](开机时间) VALUES ('@time')".Replace("@time", time);
                    SqlItem item      = new SqlItem(sqlString);
                    return(item);
                }
                else // 关机
                {
                    string time      = displayedItem.Replace(" -- ", "").TrimEnd();
                    string sqlString = "UPDATE [Table] SET 关机时间 = '@time', 时长 = '@time' - 开机时间 WHERE 序号 in (SELECT MAX(序号) FROM[Table]) "
                                       .Replace("@time", time);
                    SqlItem item = new SqlItem(sqlString);
                    return(item);
                }
            }
            // 经典模式的每条格式
            // 开机时间:2019-12-26 21:31:16.410
            // 关机时间:2019-12-26 21:31:16.410
            else if (opState == OutputState.TRADITIONAL)
            {
                if (displayedItem.Contains("开机"))
                {
                    int index = displayedItem.IndexOf(':');
                    if (index == -1)
                    {
                        throw new ArgumentException("有条目不包含:");
                    }
                    string time = displayedItem.Substring(index + 1);
                    if (UnicodeSaverUtil.IsChineseString(time))
                    {
                        throw new ArgumentException("经典模式的时间字符串存在非法字符!");
                    }
                    string  sqlString = "INSERT INTO [Table](开机时间) VALUES ('@time')".Replace("@time", time);
                    SqlItem item      = new SqlItem(sqlString);
                    return(item);
                }
                else if (displayedItem.Contains("关机"))
                {
                    int index = displayedItem.IndexOf(':');
                    if (index == -1)
                    {
                        throw new ArgumentException("有条目不包含:");
                    }
                    string time = displayedItem.Substring(index + 1);
                    if (UnicodeSaverUtil.IsChineseString(time))
                    {
                        throw new ArgumentException("经典模式的时间字符串存在非法字符!");
                    }
                    string sqlString = "UPDATE [Table] SET 关机时间 = '@time', 时长 = '@time' - 开机时间 WHERE 序号 in (SELECT MAX(序号) FROM[Table]) "
                                       .Replace("@time", time);
                    SqlItem item = new SqlItem(sqlString);
                    return(item);
                }
                else
                {
                    throw new ArgumentException("经典模式的时间字符串存在非法字符!");
                }
            }
            // 原始模式
            else if (opState == OutputState.ORIGINAL)
            {
                return(new SqlItem(displayedItem));
            }
            return(null);
        }