Read out word document fom db

Hello,

I’d like to know how I can read out a Word document from a (sql-server)database.
The document is saved in a sql-server imagefield.

For some reason, it doesn’t work (correctly), if I immediately use the stream instead of also using a byte-array.
Sometimes it goes ok, most cases I see nothing come up on my screen.

This is the code I tried:

’ -------------------------------
Dim memoryStream As New System.IO.MemoryStream(rij.Ztt_bSjabloon, 0,
rij.Ztt_bSjabloon.Length)
memoryStream.Position = 0

Try
Dim word As New Word
Dim doc As Document

Dim docBytes() As Byte = memoryStream.ToArray
Dim memStreamIn As System.IO.MemoryStream = New System.IO.MemoryStream(docBytes)

MemoryStream streamOut = new MemoryStream();
srcDoc.Save(streamOut, SaveFormat.FormatDocument);

doc = word.Open(streamOut )

doc.Save(“zittingregistraties.doc”, _
SaveFormat.FormatDocument, _
SaveType.OpenInWord, _
Me.Response _
)


Martin

What are your rij and Ztt_bSjabloon?

I suspect that somehow by the time you have your first MemoryStream created it does not contain all data.

rij.Ztt_bSjabloon is the (image)field where the document is in.

But could you please give me a small piuece of code that does what I need?
(reading out a document from a (sql-server_database))

Martin

Hi Martin,

I’ve created Documents table in my Northwind.mdb database, it has two fields Name (string) and Data (OLE Object in MS Access).

I then created this test which first loads a document into the table and then reads it back and it all works fine.

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;

using NUnit.Framework;

using Aspose.Word;

namespace Aspose.Word.Test
{
[TestFixture]
public class TestDatabaseDoc
{
///


/// Tests how documents can be stored and then loaded from a database binary field.
///

[Test]
public void TestSaveLoadDb()
{
using (OleDbConnection conn = new OleDbConnection(connString))
{
conn.Open();

//Clear the table
OleDbCommand delCmd = new OleDbCommand(“DELETE * FROM Documents”, conn);
delCmd.ExecuteNonQuery();

//Create adapter command builder and table
OleDbDataAdapter adapter = new OleDbDataAdapter(“SELECT * FROM Documents”, conn);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter);
DataTable saveDocsTable = new DataTable(“Documents”);
adapter.Fill(saveDocsTable);

//Add a document to the table first.
AddDocumentToTable(saveDocsTable, “Blank.doc”);
adapter.Update(saveDocsTable);

//Now load the table
DataTable loadDocsTable = new DataTable(“Documents”);
adapter.Fill(loadDocsTable);

//There is only one row and it contains the document we need.
DataRow row = loadDocsTable.Rows[0];
Assertion.AssertEquals(“Blank.doc”, row[“Name”]);

//The document data is the Data field.
byte[] srcDocBytes = row[“Data”] as byte[];
MemoryStream srcDocStream = new MemoryStream(srcDocBytes);

//Open the document from the memory stream.
Word word = new Word();
Document doc = word.Open(srcDocStream);
Assertion.AssertEquals(“\r\n”, doc.Range.Text);
}
}

private void AddDocumentToTable(DataTable table, string fileName)
{
DataRow row = table.NewRow();
table.Rows.Add(row);
row[“Name”] = fileName;

string fullFileName = System.IO.Path.Combine(@“X:\Aspose\Aspose.Word\Aspose.Word.Test\Testing”, fileName);
using (Stream stream = System.IO.File.OpenRead(fullFileName))
{
BinaryReader reader = new BinaryReader(stream);
row[“Data”] = reader.ReadBytes((int)stream.Length);
}
}

const string connString = @“Provider=Microsoft.Jet.OLEDB.4.0;” +
@“Data Source=X:\Aspose\Aspose.Word\Demos\Database\Northwind.mdb”;

}
}