saving/retrieving document to database

rlangi05

Member
Joined
Jan 2, 2006
Messages
21
Programming Experience
1-3
I made a similar post but I need help again... I'm trying to save a word document into an access database (to the OLE Object field) and then retrieve it and open it again.

I used a code I got for saving images to a database to save the Word document into the database:

VB.NET:
Dim da As New OleDbDataAdapter("SELECT * FROM Attachments", cnn)
        Dim command As OleDbCommandBuilder = New OleDbCommandBuilder(da)
        Dim ds As New DataSet
        Dim myRow As DataRow

        da.MissingSchemaAction = MissingSchemaAction.AddWithKey

        Dim fs As New FileStream("C:\Document.doc", FileMode.OpenOrCreate, FileAccess.Read)
        Dim MyData(fs.Length) As Byte

        fs.Read(MyData, 0, fs.Length)
        fs.Close()

        cnn.Open()

        da.Fill(ds, "Attachments")

        'add a new row to the dataTable

        myRow = ds.Tables("Attachments").NewRow()
        myRow("FileName") = "Document"
        myRow("PolicyNumber") = "20-001"
        myRow("File") = MyData

        'update the table and DataSet

        ds.Tables("Attachments").Rows.Add(myRow)
        da.Update(ds, "Attachments")

        fs = Nothing
        command = Nothing
        ds = Nothing
        da = Nothing
        cnn.Close()
        cnn = Nothing
and then the code for retrieving:

VB.NET:
Dim byteArrayOfData As Byte
        Dim bw As BinaryWriter
        Dim fs As FileStream = New FileStream("C:\Temp\Document.doc", FileMode.OpenOrCreate, FileAccess.Write)

        Dim cmdAtt As New OleDb.OleDbCommand
        Dim daAtt As New OleDb.OleDbDataAdapter
        Dim dsAtt As New DataSet

        cmdAtt = cnn.CreateCommand
        cmdAtt.CommandText = "SELECT * FROM Attachments"
        daAtt.SelectCommand = cmdAtt
        Dim cb As OleDbCommandBuilder = _
            New OleDbCommandBuilder(daAtt)
        daAtt.Fill(dsAtt, "Attachments")

        byteArrayOfData = dsAtt.Tables("Attachments").Rows(0).Item("File")

        bw = New BinaryWriter(fs)
        bw.Write(byteArrayOfData)
        bw.Flush()
        bw.Close()
        fs.Close()

        'Once the file is written,

        System.Diagnostics.ProcessStart("C:\Temp\Document.doc")
I must be doing something wrong cuz the document that it created doesnt have anything.
 
Back
Top