【问题标题】:Simulate mysql compress function using c#使用c#模拟mysql压缩功能
【发布时间】:2012-02-22 13:41:44
【问题描述】:

我想在 mysql blob 列中插入一些字节数据。 数据很大,所以我想以压缩的方式存储它。

我正在使用 Entity Framework 3.5SP1 使用最新的 mysql .net 连接器将数据插入 mysql 数据库。有没有办法使用实体框架将blob插入mysql数据库(如insert into testtable (blobcolumn) values (compress('aaa')))或在c#中模拟mysql的compress函数,然后将结果插入数据库?

谢谢

【问题讨论】:

    标签: c# .net mysql entity-framework compression


    【解决方案1】:

    在连接字符串中指定'UseCompression' 选项以启用数据包压缩。

    【讨论】:

    • 这不只是流量,而不是存储吗?
    • 这是关于流量的。一些存储引擎也提供此功能(ROW_FORMAT 表选项),例如INNODB 压缩 - dev.mysql.com/doc/innodb-plugin/1.0/en/innodb-compression.html
    • 我不想压缩整个表,而是用mysql压缩格式写一列。实际上,这意味着我想使用 .net 插入数据集并使用 mysql 的解压缩功能选择日期集。
    • 关于 c# 中的仿真:我不确定是否可能。来自 COMPRESS 函数文档:“此函数要求 MySQL 已使用 zlib 等压缩库进行编译。”所以,我们不知道确切的压缩机制。
    【解决方案2】:

    请在下面找到我使用 Ionic zip 的实现。 希望对您有所帮助!

    using System; using System.IO; using Ionic.Zlib; using System.Text; namespace Qobuz { public static class MySqlCompressHelper { public static byte[] MySqlCompress(this string str, CompressionLevel level = CompressionLevel.BestCompression) { return UTF8Encoding.UTF8.GetBytes(str).MySqlCompress(level); } public static byte[] MySqlCompress(this byte[] buffer, CompressionLevel level = CompressionLevel.BestCompression) { using (var output = new MemoryStream()) { output.Write(BitConverter.GetBytes((int)buffer.Length), 0, 4); using (var compressor = new ZlibStream(output, CompressionMode.Compress, level)) { compressor.Write(buffer, 0, buffer.Length); } return output.ToArray(); } } public static string MySqlUncompressString(this byte[] buffer) { return UTF8Encoding.UTF8.GetString(buffer.MySqlUncompressBuffer()); } public static byte[] MySqlUncompressBuffer(this byte[] buffer) { using (var output = new MemoryStream()) { using (var decompressor = new ZlibStream(output, CompressionMode.Decompress)) { decompressor.Write(buffer, 4, buffer.Length - 4); } return output.ToArray(); } } } }

    【讨论】:

      猜你喜欢
      • 2014-11-29
      • 1970-01-01
      • 2022-06-17
      • 2012-04-10
      • 1970-01-01
      • 2012-12-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多