0

Is there a way to convert bytes array returned from sql server to a stream object in c# ? I want to be able to do something like this below

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;

Is there any other way of saving it in sql server other than in a varbinary(MAX) format?

nnm
  • 1,335
  • 5
  • 18
  • 32

2 Answers2

9

it's easy. Just use MemoryStream to convert this.

Stream S = new MemoryStream(bytes);

or this.

    static void Write(Stream s, Byte[] bytes)
    {
        using (var writer = new BinaryWriter(s))
        {
            writer.Write(bytes);
        }
    }
Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40
1

You can use the MemoryStream class to convert a byte array to a Stream Object

Stream st = new MemoryStream(bytes);
aochagavia
  • 5,887
  • 5
  • 34
  • 53
Belayet
  • 55
  • 1
  • 10