Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从GZipStream获取字节数组或可读流_C#_Stream_Gzipstream - Fatal编程技术网

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

C# 如何从GZipStream获取字节数组或可读流,c#,stream,gzipstream,C#,Stream,Gzipstream,我想将字节数组的压缩版本写入SQLServerVarBinary(max)列 我想将SqlClient命令的参数输入到aSqlBytestype,并尝试实例化该类型,以便: // data is a byte array at this point SqlParameter p7 = new SqlParameter("@bytes", Compress(data)); p7.SqlDbType = SqlDbType.VarBinary; public static SqlBytes Co

我想将字节数组的压缩版本写入SQLServerVarBinary(max)列

我想将SqlClient命令的参数输入到a
SqlBytes
type,并尝试实例化该类型,以便:

// 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(myouputmorystream)
将不起作用

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);             
    }
}