Is there a way to get the binary representation of the document object for saving directly in a database?
In other words I don’t want to execute document.save and download the file to my computer, I want the server to push the generated file directly to the database.
Thanks,
Mirek
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Thanks fro your request. Yes of course you can do this. Please see the following code examples.
///
/// This example shows how to write document into the database.
///
public void Example002()
{
//Create connction
string connectionString = "server=Web1;database=TestDB;uid=sa;pwd=pass;";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
// Open the DOC file using Aspose.Words.
Document doc = new Document(@"C:\Temp\in.doc");
// ...You can merge data/manipulate document content here.
MemoryStream stream = new MemoryStream();
//Save document to memorystream
doc.Save(stream, SaveFormat.Doc);
//Create sql command
string commandString = "INSERT INTO [Documents] VALUES(@Doc)";
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);
//Add paramenter @Doc
command.Parameters.AddWithValue("Doc", stream.GetBuffer());
//Open connection
conn.Open();
//Write document to DB
command.ExecuteNonQuery();
//Close DB connection
conn.Close();
}
///
/// This example shows how to read document from the database.
///
public void Example003()
{
//Create connction
string connectionString = "server=Web1;database=TestDB;uid=sa;pwd=pass;";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
//Create DataSet
DataSet ds = new DataSet();
//Create sql command
string commandString = "SELECT Document FROM Documents WHERE ID=2";
System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandString, conn);
//Create data adapter
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(command);
//Open connection
conn.Open();
//Read dataset
adapter.Fill(ds);
conn.Close();
//Save document to hard disk
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
byte[] buffer = (byte[])ds.Tables[0].Rows[0][0];
MemoryStream stream = new MemoryStream(buffer);
Document doc = new Document(stream);
doc.Save(@"C:\Temp\out.doc");
}
}
}
I hop this could help you.
Best regards.
Thanks Alexey, this is exactly what I needed.
Mirek