Example #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());
            }
        }
Example #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);
            }
        }