Export VARBINARY to file from SQL Server

edited November 2021

I am new to Linqpad and am in the process of starting to learn some programming languages. I have been working with the following code that was recommended on Stack Exchange that allows me to convert data stored in the "Image" binary column of SQL Server to a jpg file on disk...one at at time. This is working well for me as I pull out selected images based on their ID column.

void Main()
var context = this;
var query =
from ci in context.Images
where ci.ID == 5
select ci.Image
byte[] result = query.Single().ToArray();
File.WriteAllBytes(@e:\temp\ci.5.jpg, result);

I am now being asked to just extract all of the rows and name them based on their ID. I am trying to figure out how to loop the query but as I mentioned, I am just starting to learn some programming languages, in particular C#. I truly appreciate any help to point me in the right direction!

Sign In or Register to comment.