Save created PDF to MySQL database C#

眉间皱痕 提交于 2019-12-11 09:32:18

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/28647435/save-created-pdf-to-mysql-database-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!