-1

I want to store the PDF file in the database using C# and linq.

SQL Server table:

CREATE TABLE [dbo].[FATURA_PDF]
(
    [ID] [INT] IDENTITY(1,1) NOT NULL,
    [FATURA_NO] [NVARCHAR](50) NULL,
    [PDF_IMAGE] [VARBINARY](max) NULL,
    [GORULDU] [NVARCHAR](50) NULL,
    [GUID] [UNIQUEIDENTIFIER] NULL
)

PDF image columns pdf file write / read code?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Please note Stack Overflow isn’t “write code for me” site. If you try to do it yourself and run into specific problems you can ask for help with those. As it is your question is better answered by a tutorial, many of which you’ll find with your favorite search engine. – Sami Kuhmonen Oct 13 '18 at 06:01

2 Answers2

0

Create a function to convert your file into binary then put it in your insert query :

private byte[] GetBinaryFile(filename)
{
     byte[] bytes;
     using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
     {
          bytes = new byte[file.Length];
          file.Read(bytes, 0, (int)file.Length);
     }
     return bytes;
}
Amin Mozhgani
  • 604
  • 1
  • 7
  • 22
0

try this code:

var oFATURA_PDF = new FATURA_PDF() {
            .....
        };
using (var reader = new System.IO.BinaryReader(upload.InputStream))
{
    oFATURA_PDF.PDF_IMAGE = reader.ReadBytes(upload.ContentLength);
}

db.FATURA_PDF.Add(oFATURA_PDF);
db.SaveChanges();
alireza khosravi
  • 405
  • 2
  • 17