How to save files in database....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • john20
    New Member
    • Jul 2008
    • 37

    #1

    How to save files in database....

    Hi All,

    How can i save any extension file type in the database and retreive that file when i want.

    what i mean here that i have different file types like .txt,.doc,pdf etc.

    how to save these file in database and retreive when needed and show it to user on request.

    Is it possible ? can u please give me some sample code.



    Thanks

    -yanku
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    The column-type you will store it in depends on the database you are working on. MSSQL uses "Image", MySql uses "BLOB", Access uses "OLE Object".

    But the basic idea is to use a FileStream to fill a Byte array (byte[]) and then use that byte array as a parameter for your insert query.

    Comment

    • john20
      New Member
      • Jul 2008
      • 37

      #3
      Hi thanks for your reply.

      Can u please provide some code about how to convert file into bytes stream

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        No, because we're trying to help you help yourself.

        However, I can give you a link to the MDSN Page for the FileStream object, and I can point you in the right direction:

        Here's how to create a filestream object:
        c#
        Code:
        using System.IO;
        .
        .
        .
        FileStream fs = new FileStream(@"c:\path\file.txt",FileMode.Open, FileAccess.Read);
        Change the path, FileMode, and FileAccess values as necessary, although for this task, this should be fine.

        The method you will need to use to read the file's data is called "Read".

        Comment

        Working...