The database for my current project stores pdf documents as images within the database. I needed to write a quick rough and ready program to retrieve all pdfs which meet a certain set of criteria.

The main key is remembering that a binary, or image comes back as an array of bytes. This enables us to make use of System.IO.File.WriteAllBytes which takes in a string parameter filePath representing the path to the file, and a byte[] parameter representing the file.

I set up a data set, and defined a TableAdapter MyQueryTableAdapter to have a parameterised query as MyQuery to extract the relevant fields, and then used:

MyProjectTableAdapters.MyQueryTableAdapter tableAdapter = new MyProject.MyProjectTableAdapters.MyQueryTableAdapter();
MyProject.MyQueryDataTable table;

to define table and tableAdapter variables.
These were then used to populate the table with data based upon myParameter.

table = tableAdapter.GetData(myParameter);

All I did then was loop through the table.Rows in the table and populate a byte[] myPdfDocument variable for each row. This myPdfDocument was then written using the above code.

Job done.