如何从GZipStream中获取字节数组或可读流



我想将字节数组的压缩版本写入SQL Server varbinary(max)列。

我想为SqlClient命令的参数提供SqlBytes类型,并尝试实例化该类型,因此:

// data is a byte array at this point
SqlParameter p7 = new SqlParameter("@bytes", Compress(data));
p7.SqlDbType = SqlDbType.VarBinary;

public static SqlBytes Compress(byte[] input)
{
    using (MemoryStream memstream = new MemoryStream(input))
    {    
        using (GZipStream zipped = new GZipStream(memstream, CompressionMode.Compress))
        {               
            return new SqlBytes(zipped);    
        }    
    }
}

但命令失败,并显示"不支持此操作"错误(请参阅下面的跟踪)。因此,我需要将压缩的内容从GZipStream中提取出来,并转换为允许实例化SqlBytes类型的形式。这是怎么做到的?

注意:GZipStream不支持读取,因此zipped.CopyTo( myOuputMemoryStream)将不起作用。

at System.IO.Compression.GZipStream.get_Length()
at System.Data.SqlTypes.SqlBytes.get_Value()
at System.Data.SqlClient.SqlParameter.BinarySize(Object value, Boolean isSqlType)
at System.Data.SqlClient.SqlParameter.GetActualSize()
at System.Data.SqlClient.SqlParameter.ValidateTypeLengths(Boolean yukonOrNewer)
at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at .Form1.Save2Database(Byte[] data) 
in c:\Users\foo\Documents\Visual Studio 2013\Projects\Test\Test\Form1.cs:line 228

传递到GZipStream构造函数的流是以压缩(或解压缩)格式写入数据的流。您应该创建一个空内存流,并使用GZipStream:将字节写入其中

public static SqlBytes Compress(byte[] input)
{
    using (MemoryStream memstream = new MemoryStream())
    {    
        using (GZipStream zipped = new GZipStream(memstream, CompressionMode.Compress))
        {               
            zipped.Write(input, 0, input.Length);
        }   
        return new SqlBytes(memstream);             
    }
}

相关内容

  • 没有找到相关文章

最新更新