How to insert and read a pdf file into a Sql Server 2005 database using C #

can someone tell how to insert pdf file into sqlserver 2005 and read pdf file from sqlserver ...

Thanks at advace

+5
source share
3 answers

If you are interested in using a database to store files, see this 4guysfromrolla article . It focuses on websites, but should not have problems finding what you need.

+3
source

, . , AspNetFileUploadWebControl.FileBytes. DB ( SQL "" ).

, - :

theRow = getDatarowFromDatabase();
aByteArrayOfTheFile = (byte[])theRow["theSqlImageColumnWithTheFileInIt"];

, SendAsFileToBrowser():

SendAsFileToBrowser(aByteArrayOfTheFile, "application/pdf", "downloaded.pdf");

( ):

    // Stream a binary file to the user web browser so they can open or save it.
    public static void SendAsFileToBrowser(byte[] File, string Type, string FileName)
    {
        string disp = "attachment";
        if (string.IsNullOrEmpty(FileName))
        {
            disp = "inline";
        }

        // set headers
        var r = HttpContext.Current.Response;
        r.ContentType = Type; // eg "image/Png"
        r.Clear();
        r.AddHeader("Content-Type", "binary/octet-stream");
        r.AddHeader("Content-Length", File.Length.ToString());
        r.AddHeader("Content-Disposition", disp + "; filename=" + FileName + "; size=" + File.Length.ToString());
        r.Flush();

        // write data to requesting browser
        r.BinaryWrite(File);
        r.Flush();
    }
    //overload
    public static void SendAsFileToBrowser(byte[] File, string Type)
    {
        SendAsFileToBrowser(File, Type, "");
    }
    // overload
    public static void SendAsFileToBrowser(System.IO.Stream File, string Type, string FileName)
    {
        byte[] buffer = new byte[File.Length];
        int length = (int)File.Length;
        File.Write(buffer, 0, length - 1);
        SendAsFileToBrowser(buffer, FileName, Type);
    }

>

+3

Essentially, you're just talking about storing and retrieving BLOBs (from imageor data varbinary(max)). See This Question: Streaming Directly to a Database

0
source

All Articles