Lloyd
Lloyd

Reputation: 403

Save created PDF to MySQL database C#

I want to be able to store newly created PDF in a BLOB field in my MySQL database. This is the code I have so far but it saves to local disk instead:

        string fileName = employeeNo.ToString();
        MemoryStream ms = new MemoryStream();
        byte[] bits = new byte[0];

        Document doc = new Document();
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        doc.Open();
        iTextSharp.text.Rectangle rec2 = new iTextSharp.text.Rectangle(PageSize.A4);
        doc.Add(new Paragraph(textBox2.Text));
        doc.Add(new Paragraph(textBox1.Text));
        doc.Close();
        bits.ToArray();

How could I adapt this so that it can be inserted into my database instead? The file would then be passed into the insert statement as below:

myCmd.Parameters.AddWithValue("@feedbackComments", bits);

Upvotes: 1

Views: 1462

Answers (1)

Paulo Soares
Paulo Soares

Reputation: 1916

Instead of a FileStream use a MemoryStream and extract the byte array with ToArray() after closing the doc.

Use the byte array to populate the table.

Upvotes: 1

Related Questions