Insert PDF in database c#

  • 2.5K Views
  • Last Post 06 August 2018
  • Topic Is Solved
Stylus STYLUS posted this 06 August 2018

Insert a pdf file into sql database

private void Button2_Click(object sender, EventArgs e)   //Save pdf in database
{
    byte[] filedata = null;
    MemoryStream ms = new MemoryStream();
    filedata = ms.GetBuffer();
    axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());
    using (SqlConnection openCon = new SqlConnection(cs))
    {
        string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @[email protected]+1; INSERT into dbo.documents (number,file_location, pdf_file) VALUES (@maxNo,@file_location,@pdf_file)";
        using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
        {
            querySaveStaff.Connection = openCon;
            querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;
            querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = filedata;
            openCon.Open();
            querySaveStaff.ExecuteNonQuery();
            openCon.Close();
        }
    }
}
Order By: Standard | Newest | Votes
lucy posted this 06 August 2018

I think you should write

private void Button2_Click(object sender, EventArgs e)   //Save pdf in database
{
    OpenFileDialog ofd = new OpenFileDialog() { Filter = "PDF|*.pdf" };//Open pdf file
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        FileStream fs = File.OpenRead(ofd.FileName);
        MemoryStream ms = new MemoryStream();
        fs.CopyTo(ms);
        using (SqlConnection openCon = new SqlConnection(cs))
        {
            if (openCon.State == ConnectionState.Closed)
                openCon.Open();
            string saveStaff = "declare @maxNo integer = 0 select @maxNo = isnull(max(number), 0) from [dbo].[documents]; Set @[email protected]+1; INSERT into dbo.documents (number,file_location, pdf_file) VALUES (@maxNo,@file_location,@pdf_file)";
            using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
            {
                querySaveStaff.Connection = openCon;
                querySaveStaff.Parameters.Add("@file_location", SqlDbType.VarChar, 255).Value = file_locationTextBox.Text;
                querySaveStaff.Parameters.AddWithValue("@pdf_file", SqlDbType.VarBinary).Value = ms.ToArray();
                querySaveStaff.ExecuteNonQuery();
            }
        }
    }
}

You can use FileStream to read a pdf file, then copy pdf data to MemoryStream. To save pdf to your database, you only need to convert MemoryStream to byte array.

  • Liked by
  • Stylus STYLUS
Stylus STYLUS posted this 06 August 2018

Thank you...

 axAcroPDF1.src = LocalEncoding.GetString(ms.ToArray());

Why jump this line to show pdf in adobe acrobar on form?

How to retrieve this file from database and shoh him in adobe acrobat on windosw form?

Stylus STYLUS posted this 06 August 2018

Great. Now is file saved in sql OK.

How to retrieve file from sql and show it in axAcroPDF1 on datagrid selection changed?

Thank you

Close