Storing documents in SQL Server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Storing documents in SQL Server

    I need to store a variety of items in SQL Server. These could be text
    files, pictures, word documents, PDF, etc. I have set up my database
    to accept a varbinary(max) and I can put the items into the database
    as follows:

    Dim filename As String
    OpenFileDialog1 .ShowDialog()
    filename = OpenFileDialog1 .FileName
    Dim fs As New FileStream(file name, FileMode.Open,
    FileAccess.Read )
    Dim ilen As Integer = CInt(fs.Length)
    Dim bFile(ilen) As Byte
    fs.Read(bFile, 0, ilen)
    fs.Close()

    I then call the code to put the item into the database using a binary
    type in the System.Data.Gen eric namespace. The item is in the
    database. In this case it was a word document.

    Retreiving the document is use the following code:
    For Each rw As DataRow In dt.Rows
    Dim bFile() As Byte = rw.Item(1)
    Dim ms As New MemoryStream(bF ile)
    Dim data As String = Encoding.ASCII. GetString(bFile )

    Next

    The string in data comes back as "??????

    Does anyone have any idea or code that will convert a word doc back
    from binary? Or is there a better way to store word documents in SQL
    Server. Understand that we must store the documents in the database
    because we have to go to one source for all the data during the
    certification process so the file has to be in the database.

    Thanks.

    John
  • Michel Posseth  [MCP]

    #2
    Re: Storing documents in SQL Server

    Hello

    Also store in the database the file name and extension , just read it as
    binary write it to a file as binary and give it back its original name and
    extension
    this way you can save and retrieve anny file in SQL

    i would use a binary reader and writer for this purpose

    HTH

    Michel Posseth [MCP]






    "John" <riley_wright@h otmail.comschre ef in bericht
    news:68b18404-47fa-4702-807b-39a8f62c286d@d3 1g2000hsg.googl egroups.com...
    I need to store a variety of items in SQL Server. These could be text
    files, pictures, word documents, PDF, etc. I have set up my database
    to accept a varbinary(max) and I can put the items into the database
    as follows:

    Dim filename As String
    OpenFileDialog1 .ShowDialog()
    filename = OpenFileDialog1 .FileName
    Dim fs As New FileStream(file name, FileMode.Open,
    FileAccess.Read )
    Dim ilen As Integer = CInt(fs.Length)
    Dim bFile(ilen) As Byte
    fs.Read(bFile, 0, ilen)
    fs.Close()

    I then call the code to put the item into the database using a binary
    type in the System.Data.Gen eric namespace. The item is in the
    database. In this case it was a word document.

    Retreiving the document is use the following code:
    For Each rw As DataRow In dt.Rows
    Dim bFile() As Byte = rw.Item(1)
    Dim ms As New MemoryStream(bF ile)
    Dim data As String = Encoding.ASCII. GetString(bFile )

    Next

    The string in data comes back as "??????

    Does anyone have any idea or code that will convert a word doc back
    from binary? Or is there a better way to store word documents in SQL
    Server. Understand that we must store the documents in the database
    because we have to go to one source for all the data during the
    certification process so the file has to be in the database.

    Thanks.

    John


    Comment

    Working...