예제 #1
0
        /// <summary>
        /// 以最简单的方式系列化为Bytes 的格式。
        /// </summary>
        /// <param name="dtData"></param>
        /// <returns></returns>
        public byte[] Serializer(DataTable dtData)
        {
            List <ColumnInfo> cols = new List <ColumnInfo>();

            foreach (DataColumn col in dtData.Columns)
            {
                cols.Add(new ColumnInfo(col.ColumnName, col.DataType));
            }
            TextTableSchema schema = new TextTableSchema();
            string          flag   = schema.ColumnsToString(cols);
            string          header = buildTableHeader(cols);

            DataRow[] drs = dtData.Select();
            using (MemoryStream stream = new MemoryStream()) {
                flag = string.Format(FLAG_WORD, DATA_TABLE) + ";" + string.Format(SE_COLUMNS, flag);
                byte[] bufferflag = _Encoding.GetBytes(flag + "\n");
                stream.Write(bufferflag, 0, bufferflag.Length);
                byte[] buffer = _Encoding.GetBytes(header + "\n");
                stream.Write(buffer, 0, buffer.Length);
                foreach (DataRow dr in drs)
                {
                    string line = buildDataRow(cols, dr);
                    buffer = _Encoding.GetBytes(line + "\n");
                    stream.Write(buffer, 0, buffer.Length);
                }
                //压缩
                byte[] bytes_c = MB.Util.Compression.Instance.Zip(stream.ToArray());
                return(bytes_c);

                return(stream.ToArray());
            }
        }
예제 #2
0
        /// <summary>
        ///  以最简单的方式系列化为Bytes 的格式。
        /// </summary>
        /// <param name="dataReader"></param>
        /// <returns></returns>
        public byte[] Serializer(IDataReader dataReader)
        {
            //ColumnName
            DataTable headerReader = dataReader.GetSchemaTable();

            //构建表头:
            DataRow[]         heads = headerReader.Select();
            List <ColumnInfo> cols  = new List <ColumnInfo>();

            foreach (DataRow dr in heads)
            {
                cols.Add(new ColumnInfo(dr["ColumnName"].ToString(), (Type)dr["DataType"]));
            }
            TextTableSchema schema = new TextTableSchema();
            string          flag   = schema.ColumnsToString(cols);
            string          header = buildTableHeader(cols);

            using (MemoryStream stream = new MemoryStream()) {
                flag = string.Format(FLAG_WORD, DATA_TABLE) + ";" + string.Format(SE_COLUMNS, flag);
                byte[] bufferflag = _Encoding.GetBytes(flag + "\n");
                stream.Write(bufferflag, 0, bufferflag.Length);
                byte[] buffer = _Encoding.GetBytes(header + "\n");
                stream.Write(buffer, 0, buffer.Length);
                while (dataReader.Read())
                {
                    string line = buildDataRow(cols, dataReader);

                    buffer = _Encoding.GetBytes(line + "\n");
                    stream.Write(buffer, 0, buffer.Length);
                }

                dataReader.Close();

                //压缩
                byte[] bytes_c = MB.Util.Compression.Instance.Zip(stream.ToArray());
                return(bytes_c);
            }
        }
예제 #3
0
 public MultiSetTableValue(string colstring, List <string> lstDataRow)
 {
     schema       = new TextTableSchema();
     _LstDataRows = lstDataRow;
     _Cols        = schema.StringToColumns(colstring);
 }
예제 #4
0
        /// <summary>
        /// 以最简单的方式系列化为Bytes 的格式。
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public DataTable DeSerializer(byte[] datas)
        {
            TextTableSchema schema = new TextTableSchema();

            byte[]        bytes_c = MB.Util.Compression.Instance.UnZip(datas);
            string        dstr    = _Encoding.GetString(bytes_c);
            string        colString;
            string        header;
            List <string> dataRows = new List <string>();

            using (StringReader reader = new StringReader(dstr)) {
                //先取Flag  第一行的标志行 为构造DataTable
                string[] flag = reader.ReadLine().Split(';');
                if (flag.Length < 2)
                {
                    throw new MB.Util.APPException("反系列化时出错,不是有效的系列化文件", MB.Util.APPMessageType.SysErrInfo);
                }

                colString = flag[1].Substring(SE_COLUMNS.Length - 3, flag[1].Length - SE_COLUMNS.Length + 3);

                //先读取表头
                header = reader.ReadLine();
                string[] colFields = System.Text.RegularExpressions.Regex.Split(header, "\t");
                while (true)
                {
                    string dataRow = reader.ReadLine();
                    if (string.IsNullOrEmpty(dataRow))
                    {
                        break;
                    }
                    dataRows.Add(dataRow);
                }
            }
            int       all  = computeCount(dataRows.Count);
            var       cols = schema.StringToColumns(colString);
            DataTable dt   = schema.CreateDataTable(cols);
            List <MultiSetTableValue> lst = new List <MultiSetTableValue>();

            for (int i = 0; i < all; i++)
            {
                int           l        = (i + 1) * SINGLE_L > dataRows.Count ? dataRows.Count - i * SINGLE_L : SINGLE_L;
                List <string> childLst = dataRows.GetRange(i * SINGLE_L, l);
                lst.Add(new MultiSetTableValue(colString, childLst));
                Thread t = new Thread(new ThreadStart(lst[i].SetValue));
                t.Priority = ThreadPriority.Highest;
                t.Start();
            }
            ArrayList lstData = new ArrayList();

            for (int i = 0; i < lst.Count; i++)
            {
                if (lst[i].Done)      //保证按系列化前的顺序加载到集合中。
                {
                    dt.Merge(lst[i].CreateDataTable);
                }
                else
                {
                    i--;
                    Thread.Sleep(100);
                }
            }
            return(dt);
        }