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?

0

2 Answers 2

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);
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

Stream st = new MemoryStream(bytes);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.