有没有办法将从sql server返回的字节数组转换为c#中的流对象? 我希望能够在下面做这样的事情
FileStream fs = new FileStream(@"C:\Users\sample\sample\sample.txt", FileMode.Open, FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//bytes array will be saved in the database
//The following will be returned from the database and i need it to be in a stream
Stream s = (Stream)bytes;
除了varbinary(MAX)格式之外,还有其他方法可以将它保存在sql server中吗?
答案 0 :(得分:3)
MemoryStream
进行转换即可。
Stream S = new MemoryStream(bytes);
或者这个。
static void Write(Stream s, Byte[] bytes)
{
using (var writer = new BinaryWriter(s))
{
writer.Write(bytes);
}
}
答案 1 :(得分:0)
您可以使用MemoryStream
类将字节数组转换为Stream
对象
Stream st = new MemoryStream(bytes);