c#对dataset和list集合压缩和解压,能提高访问速度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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;
     }
 
 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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   topguntopgun  阅读(759)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示