[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Retrieving binary files from a database

Posted on 2006-05-31
Medium Priority
Last Modified: 2010-04-16
Hi all, I am writing a class file which is trying to retrieve a binary file from a sql database, although I have done this before, but al not familiar with how to handle this in a class file. I need to ave the code chec for the existance of the file and then write it to the location which will be passed in, any help would be appreciated, as always.

Thanks in advance

                    // Build Connection String
                    string connstring = "server=" + dbserver + ";uid=" + dbuser + ";pwd=" + dbpass + ";database=" + dbname;
                    string ostring = sqlquery;

                    SqlConnection oconn = new SqlConnection(connstring);
                    SqlCommand command = new SqlCommand(ostring, oconn);
                    SqlDataReader dr = command.ExecuteReader();              

                    if (dr.Read()) // we found our image

Question by:CJSantora
  • 5
  • 3

Expert Comment

ID: 16799304
try using


Pictures are stored in memory as a byte[]

Author Comment

ID: 16822890
I apologize for the slow response and and have gone to the link you provided GENTP.  I am a little unfamiliar with this and I am trying to write peice of code that will retreive a binary file (jpg or pdf) from a sql database and I would like this compiled into a DLL. The dll will be called in a coldfusion CFX tag. I started this project by creating a new console application and with that type of application it appears that the Object Bitmap is unavailable.

So I have a couple questions, first am I using the correct type of application to create a DLL, I am using Visual studio 2005 and C#.
Second, if so, is there a way to referenece the Bitmap object?

Code below:
     if (b.Length > 0)
                        // Open a stream for the image and write the bytes into it
                        System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
                        stream.Write(b, 0, b.Length);

                        // Create a bitmap from the stream
                        Bitmap bmp = new Bitmap(stream);    // THE ERROR IS THROWN HERE

Thanks for your help
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 48

Expert Comment

ID: 16822966
What error?

Author Comment

ID: 16823016
The error was being thown because I needed to add areference to System.Drawing, I have already corretced that. I am still unsure of the rest of the question above and how to determine the fileType that is being returned, I want to validate that the binary file being returned is either a JPG or a PDF. By using:
bmp.Save(destfilepath + "\\temp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

I can save the jpg file to disk, but what about the pdf file and how do I determine the file type prior to saving?

Thanks again
LVL 48

Accepted Solution

AlexFM earned 1000 total points
ID: 16823417
You need to define some rule for this, for example, add database field "File type". Fill it when file is written to database, and use it when file is loaded from database.
To save pdf file from database to disk, you can use BinaryWriter class instead of Bitmap. You can create BinaryWriter from MemoryStream exactly like code sample creates Bitmap from MemoryStream.
LVL 48

Expert Comment

ID: 16998760
Without questioners response, I suggest split.
LVL 48

Expert Comment

ID: 16999085
Do you really think I need your C grade? If you don't like the answer, ask Community Support to close question.

Author Comment

ID: 16999151
I was not trying to upset anyone, rather I was trying to award the points for your attempt to help. The grade was given because it did not resolve my issue, if I handled this incorrectly that was not my intention, instead I was trying to show a award of points for your attempt.

Possibly I do not understand how all this affects each of the participants.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question