c#对dataset和list集合压缩和解压,能提高访问速度
public class YS { public static byte[] Decompress(byte[] data) { byte[] bData; MemoryStream ms = new MemoryStream(); //把数据写入到内存流 ms.Write(data, 0, data.Length); //确定写入的位置,因为不是一次性写入 ms.Position = 0; //解压流 GZipStream stream = new GZipStream(ms, CompressionMode.Decompress, true); //性能处理 byte[] buffer = new byte[4096]; //临时内存流 MemoryStream temp = new MemoryStream(); //读入指定的文件流,0表示有没有到文件末尾 int read = stream.Read(buffer, 0, buffer.Length); while (read > 0) { //写入到临时缓冲区内 temp.Write(buffer, 0, read); // read = stream.Read(buffer, 0, buffer.Length); } //必须把stream流关闭才能返回ms流数据,不然数据会不完整 stream.Close(); stream.Dispose(); ms.Close(); ms.Dispose(); bData = temp.ToArray(); temp.Close(); temp.Dispose(); return bData; } /// <summary> /// 压缩数据 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] Compress(byte[] data) { byte[] bData; //创建内存流 MemoryStream ms = new MemoryStream(); //创建压缩解压对象 GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true); //把数据写入缓冲区 stream.Write(data, 0, data.Length); stream.Close(); stream.Dispose(); //必须把stream流关闭才能返回ms流数据,不然数据会不完整 //并且解压缩方法stream.Read(buffer, 0, buffer.Length)时会返回0 bData = ms.ToArray(); ms.Close(); ms.Dispose(); return bData; } public static DataSet ConvertByteArrayToDataSet(Byte[] aByte) { DataSet resultDs = null; MemoryStream ms = new MemoryStream(aByte); IFormatter bf = new BinaryFormatter(); try { object obj = bf.Deserialize(ms); resultDs = (DataSet)obj; ms.Close(); return resultDs; } catch (Exception ex) { throw ex; } finally { if (ms != null) ms.Close(); resultDs.Dispose(); } } /// <summary> /// 将DataSet格式化成字节数组byte[] /// </summary> /// <param name="dsOriginal">DataSet对象</param> /// <returns>字节数组</returns> public static Byte[] ConvertDataSetToByteArray(DataSet aDs) { //保存序列化的对象 Byte[] tranData; //创建内存流 MemoryStream ms = new MemoryStream(); //创建格式化对象 IFormatter bf = new BinaryFormatter(); try { if (aDs == null) return null; //设置序列化的方式 aDs.RemotingFormat = SerializationFormat.Binary; //序列化字节对象 bf.Serialize(ms, aDs); //把流转换成字节数组 tranData = ms.ToArray(); ms.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (ms != null) ms.Close(); } return tranData; } /// <summary> /// 把list转换成dataset /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <returns></returns> public static DataSet ConvertToDataSet<T>(IList<T> list) { if (list == null || list.Count <= 0) { return null; } //创建dataset 对象 DataSet ds = new DataSet(); //创建表的,并且以类型的名字命名 DataTable dt = new DataTable(typeof(T).Name); DataColumn column; DataRow row; //通过反射得到属性对象,只能得到简单的属性 System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); //遍历集合,遍历一次创建一行 foreach (T t in list) { if (t == null) { continue; } row = dt.NewRow(); //根据属性创建列 for (int i = 0, j = myPropertyInfo.Length; i < j; i++) { System.Reflection.PropertyInfo pi = myPropertyInfo[i]; string name = pi.Name; if (name.Trim().ToLower().Equals("lastmodify")) { continue; } //判断列的值为不为空 if (dt.Columns[name] == null) { //获得属性的类型,作为列的类型 Type colType = pi.PropertyType; //感觉这个if语句没啥用,有知道的朋友给我留言,谢谢了 if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) { colType = colType.GetGenericArguments()[0]; } column = new DataColumn(name, colType); dt.Columns.Add(column); } //获得属性的值,判断是不是为空 row[name] = pi.GetValue(t, null) == null ? DBNull.Value : pi.GetValue(t, null); } dt.Rows.Add(row); } ds.Tables.Add(dt); return ds; } public static List<T> GetList<T>(DataSet ds ) { DataTable table = ds.Tables[0]; List<T> list = new List<T>(); // T t = default(T); PropertyInfo[] propertypes = null; string tempName = string.Empty; try { foreach (DataRow row in table.Rows) { //动态创建一个对象 t = Activator.CreateInstance<T>(); //获得该类的所有属性 propertypes = t.GetType().GetProperties(); //遍历属性 foreach (PropertyInfo pro in propertypes) { //属性名 tempName = pro.Name; //检查表中是否包含该列 if (table.Columns.Contains(tempName)) { object value = row[tempName]; //值不能为空, if (value != null && value != DBNull.Value && row[tempName].ToString() != null && !row[tempName].ToString().Trim().Equals("")) { if (tempName.Trim().ToLower().Equals("lastmodify")) { // pro.SetValue(t, ConvertHelper.ConvertToTimestamp(Convert.ToString(value)), null); } else { //char类型单独处理 if (pro.PropertyType == typeof(System.Char) || pro.PropertyType == typeof(System.Nullable<System.Char>)) { pro.SetValue(t, Convert.ToChar(value), null); } else { pro.SetValue(t, value, null); } } } //if (value.GetType() == typeof(System.DBNull)) //{ // value = null; //} //if (tempName.Trim().Equals("lastmodify")) //{ // pro.SetValue(t,Convert.to //pro.SetValue(t, 0, null); //} //else //{ // pro.SetValue(t, value, null); //} } } list.Add(t); } } catch (Exception ex) { throw new Exception(ex.Message); } return list; } }
static void Main(string[] args) { // using (BookShop2Entities context = new BookShop2Entities()) { List<User<string>> list = new List<User<string>>{ new User<string>(){Age=90} }; //把list集合转化成dataset DataSet ds = YS.ConvertToDataSet(list); //压缩dataset byte[] byes =YS.ConvertDataSetToByteArray(ds); Console.WriteLine("压缩之前字节数"+byes.Length); byte[] byesYS =YS.Compress(byes); Console.WriteLine("压缩之后字节数" + byesYS.Length); byte[] byesJZ = YS.Decompress(byesYS); Console.WriteLine("解压之后字节数" + byesJZ.Length); //解压成dataset DataSet ds2 = YS.ConvertByteArrayToDataSet(byesJZ); // Console.WriteLine("完成"); Console.Read(); }
这个自己写的,有什么问题可以给我留言
posted on 2014-09-23 16:54 topguntopgun 阅读(752) 评论(0) 编辑 收藏 举报