Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 17:10pm

1 solution

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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900