Can Sql Server store pdfs?

VBAHole22

Well-known member
Joined
Oct 21, 2003
Messages
432
Location
VA
Streaming PDFs back and forth to a db

I have a little tool I created in a win form that allows users to view images that are stored in a sql server db as blobs. They pick a name and the blob gets shown. The images are jpegs that I loaded into sql using a different tool that I have.

My question is can I do this same procedure with pdf files? Can they be crammed into sql and read back out?
The way I am doing the reading now is like so:

Code:
byte[] b = (byte [])com.ExecuteScalar();
MemoryStream mem = new MemoryStream(b);
pictureBox1.Image = Image.FromStream(mem);

So I query the db and then stream out the image to a picturebox.
What are the limitations of this method? Can it be done on jpeg, .tif, .bmp, pdf?
 
You could just store the PDF as a blob in the DB, that way you would be able to store and retreive it using the same code you are using with a jpeg.
Depending on how you want to view the file though will probably require you saving it out as a physical file rather that being able to use a memorystream.
 
I think showing the pdf to the user is going to be the tough part. I dont mind having reader pop up if I can get that to work.
Another issue I have is this: I have jpegs and pdfs in the same image datatype field. How can I snoop to figure out which one is coming through the pipe? Or maybe I dont need to because whatever handles the file will try to open it?
 
The following line of code will pop open the application associated a given file type:

Code:
System.Diagnostics.Process.Start(imageFilename)
 
From a webserver, you can stream a PDF. Youll have to work out how to convert your blob into a Byte array, but that shouldnt be hard. Then do this:
C#:
Response.ContentType = "application/pdf";
byte[] document = GetPDFDocument();
Response.BinaryWrite(document);
Response.End();

When the user goes to your aspx page, it will open in Adobe reader, usually within the browser. The user has the option (clientside) to force Adobe to always open by itself (not in a browser). The above code will still work, it just wont open in a browser. Thats a user preference, so theres nothing you can do about it.

This is how Ive streamed PDFs. As with anything, Im sure there are other ways to do it.

-ner
 
This is for a win app so I dont think I get response type and stuff like that.

I have had a little success using

Code:
				cc = new SqlConnection(connString);
				com = new SqlCommand(cmd,cc);
				cc.Open();
				byte[] b = (byte [])com.ExecuteScalar();
				MemoryStream mem = new MemoryStream(b);
				string tempPath = Path.GetTempPath();
				tempPath += "\\TempPDF.pdf";
				System.IO.FileStream fs = new System.IO.FileStream(tempPath, FileMode.CreateNew);
				BinaryWriter w = new BinaryWriter(fs);
				w.Write(b);
				w.Close();
				fs.Close();
				this.axPdf1.LoadFile(tempPath);

where axPdf1 is the Adobe ActiveX component. The file gets created but then I cant open it in reader (via win explorer) because it says it is either corrupt or not a supported file type.

Inside my win app form I get an error about the file not being correct.
 
VBAHole22 said:
This is for a win app so I dont think I get response type and stuff like that.

I have had a little success using

Code:
				cc = new SqlConnection(connString);
				com = new SqlCommand(cmd,cc);
				cc.Open();
				byte[] b = (byte [])com.ExecuteScalar();
				MemoryStream mem = new MemoryStream(b);
				string tempPath = Path.GetTempPath();
				tempPath += "\\TempPDF.pdf";
				System.IO.FileStream fs = new System.IO.FileStream(tempPath, FileMode.CreateNew);
				BinaryWriter w = new BinaryWriter(fs);
				w.Write(b);
				w.Close();
				fs.Close();
				this.axPdf1.LoadFile(tempPath);

where axPdf1 is the Adobe ActiveX component. The file gets created but then I cant open it in reader (via win explorer) because it says it is either corrupt or not a supported file type.

Inside my win app form I get an error about the file not being correct.

I always make it a habit to explicitly call Flush() before closing the stream -- Ive had too many corrupted files without doing so...
 
Back
Top