1

I am currently working on a windows form application and I need to create functionality that enables users to upload PDF files to MySQL, and also be able to read the PDF file from the MySQL database then display the PDF file in the window form.

Can anyone show me some example code for how to do this?

casperOne
  • 73,706
  • 19
  • 184
  • 253
user920394
  • 19
  • 1
  • 2
  • 1
    Storing big blobs in a database leads to suffering, I suggest storing the filename in the database and the actual PDF in the filesystem. – Johan Aug 30 '11 at 19:47
  • look at this http://stackoverflow.com/questions/4504442/viewing-pdf-in-windows-forms-using-c and http://stackoverflow.com/questions/983726/store-a-pdf-in-mysql – andres descalzo Aug 30 '11 at 19:49
  • Are you storing file in mysql ? You should rather store filename . Or use some primary key field to get filename . – Pit Digger Aug 30 '11 at 19:52

1 Answers1

1

You can store the pdf in a mysql database inside a column (varbinary(MAX)) with a byte[], to build byte[] try this:

byte[] bytes = null;
try
{
 bytes = File.ReadAllBytes(fileName);
}
catch (IOException)
        {
            ...      
        }

filename is the pdf name with the path. After that build some sql query to insert the byte[] and to get the byte[]

To show the pdf you need to convert byte[] to file like this:

Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (Stream file = File.Create(fileName))
{
file.Write(buffer, 0, buffer.Length);
}

buffer is the byte[]

then finally if you want to open the pdf:

Process process = new Process();
process.StartInfo.FileName = path;
process.Start();

path is the pdf name with the path.

Guillaume V
  • 931
  • 2
  • 10
  • 27