Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what is datatype for store "pdf" file in sql database
Posted

Hi,

if you want to store your file in sql server database, you can use Varbinary type for this.

you can try following piece of code and table structure for your purpose, I hope it matches your requirements.



Table structure:
SQL
CREATE TABLE [dbo].[PdfFiles](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Type] [varchar](50) NULL,
    [Data] [varbinary](max) NULL
) ON [PRIMARY]



Code on button click:

C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            string filePath = FileUpload1.PostedFile.FileName;          // getting the file path of uploaded file
            string filename = Path.GetFileName(filePath);               // getting the file name of uploaded file
            string ext = Path.GetExtension(filename);                      // getting the file extension of uploaded file
            string type = String.Empty;

            if (!FileUpload1.HasFile)
            {
                Label2.ForeColor = System.Drawing.Color.Red;
                Label2.Text = "Please Select a File";
                
            }
            else if (FileUpload1.HasFile)
            {
                switch (ext)
                {
                    case ".pdf":
                        type = "application/pdf";
                        break;
                    case ".txt":
                        type = "application/txt";
                        break;
                    case ".docx":
                        type = "application/docx";
                        break;
                    

                }

                if (type != string.Empty)
                {
                    SqlConnection conn = new SqlConnection(con);

                    Stream fs = FileUpload1.PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);                                 //reads the   binary files
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);                           //counting the file length into bytes

                    string query = "insert into PdfFiles (Name,type,data)" + " values (@Name, @type, @Data)";   //insert query
                    SqlCommand com = new SqlCommand(query, conn);
                    com.Parameters.AddWithValue("@Name", filename);
                    com.Parameters.AddWithValue("@type", type);
                    com.Parameters.AddWithValue("@Data", bytes);
                    conn.Open();
                    com.ExecuteNonQuery();
                    conn.Close();
                    Label2.ForeColor = System.Drawing.Color.Green;
                    Label2.Text = "File Uploaded Successfully";

                }
                else
                {
                    Label2.ForeColor = System.Drawing.Color.Red;
                    Label2.Text = "Please select a pdf/txt/word file";

                }
            }



        }





Happy coding :)
 
Share this answer
 
You can use varbinary data type. You have to convert PDF file into byte array, please refer -> http://stackoverflow.com/questions/14770228/how-to-convert-a-file-of-any-type-into-byte-array[^]


Pass this byte array to varbinarycolumn.
you can use simple sql statement for insert/ updated pdf file.
 
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