Convert varbinary back to .txt file

Robin picture Robin · Oct 18, 2011 · Viewed 11.5k times · Source

I have a database (SQL 2008) where I store file's in. These are saved as a varbinary(max) type.

Now I need to get the .txt file again so I can loop through the contents of the file like i used to do with StreamReader.

while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}

But how can I convert the varbinary byte[] back to the normal .txt file so I'm able to go through the contents line by line.

I found some ideas with memorystream or filestream but can't get them to work.

Thanks in advance!

Answer

Ovidiu Pacurar picture Ovidiu Pacurar · Oct 18, 2011
MemoryStream m = new MemoryStream(byteArrayFromDB);
StreamReader file = new StreamReader(m);
while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}