-2

I am using C#. I want to store PDF files from a Windows forms application to a SQL database. And I want to retrieve it to my Windows Forms application again. What controls can I use to upload PDF files (for example for image I use picture box). What is the data type for PDF files?

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bimal
  • 45
  • 1
  • 1
  • 3

5 Answers5

4

To select a file you can use OpenFileDialog

Saving any file into database

Community
  • 1
  • 1
Mihai Hantea
  • 1,741
  • 13
  • 16
-1

You'd better store pdf in file storage and save link to it in database.

Redwan
  • 738
  • 9
  • 28
  • I hear varying arguments about this. Apparently there is a breaking point at 256kb where the pdf gets fragmented in the database then affects performance and you should switch to file storage. But I don't see pdf documents as being over the 256kb limit. – Timothy Gonzalez May 29 '17 at 20:31
  • If you work with light pdf like application forms 256kb that's ok. But when someone starts upload 60mb scans you become worried. – Redwan May 29 '17 at 20:41
-1

To store a PDF file in your SQL Server Database, you need to store the contents of the file in BinaryData field. However, I am sure that you will face so many problem with this setup, that you would do better to save your file contents in the SystemData folder and just store the file path or name in to DB.

This will be much more reliable and easy to maintain.

Another approach if your application is storing only files, is to use MongoDb instead of SQL Server. I am not familiar with that, but quite sure that this will be an easy and good solution.

Daniel Von Fange
  • 857
  • 9
  • 10
Sagar Upadhyay
  • 819
  • 2
  • 11
  • 31
  • mangDb? and why you "quit sure that this will be easy and good solutions." MongoDB is a document database but that does not mean that it stores pdf documents – Ash Apr 16 '15 at 12:45
-1

There is an app in the Windows App Store specifically for storing PDF file info and PDF metadata in a SQL database. You can then search and filter that database using simple, standard SQL queries.

https://www.microsoft.com/en-us/store/p/pdfdb/9n4z1l2hc2c0

Mah Moh
  • 1
  • 2
-1
public void getTestReportDocument(string reportid, string extenstype)
{

    try
    {
        string filesName = "";
        if (sqlConn.State == ConnectionState.Closed)
            sqlConn.Open();

        if(extenstype == ".pdf")
        {
            filesName = Path.GetTempFileName();

        }
        else
        {
            filesName = Path.GetTempFileName() + extenstype;
        }


        SqlCommand cmd = new SqlCommand("GetTestReportDocuments", sqlConn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ReportID", reportid);

        using (SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.Default))
        {
            while (dr.Read())
            {
                int size = 1024 * 1024;
                byte[] buffer = new byte[size];
                int readBytes = 0;
                int index = 0;
                using (FileStream fs = new FileStream(filesName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
                    {
                        fs.Write(buffer, 0, readBytes);
                        index += readBytes;

                    }
                }

            }
        }
        Process prc = new Process();
        prc.StartInfo.FileName = filesName;
        prc.Start();

    }

    catch (Exception ex)
    {
        throw ex;
    }

    finally
    {
        //daDiagnosis.Dispose();
        //daDiagnosis = null;
    }

}

Take a look at this screenshot for the solution.

Matthew
  • 1,905
  • 3
  • 19
  • 26
rakesh
  • 1
  • 1