Storing documents in SQL Server

J

John

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(filename, 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.Generic 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(bFile)
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
 
M

Michel Posseth [MCP]

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]
http://www.vbdotnetcoder.com





"John" <[email protected]> schreef in bericht
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(filename, 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.Generic 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(bFile)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top