Click here to Skip to main content
14,454,912 members
Rate this:
Please Sign up or sign in to vote.
See more:
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


What I have tried:

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;
    }

}
Posted
Updated 14-Feb-20 18:10pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Windows Forms applications generally do not need an "upload" control as they are generally run where the file is located; you just need to use an "Open File" dialog control.

Plenty of samples for doing this on the internet as well as here
Save and view pdf file from SQL server database in c# WinForms[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100