We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Save and Retrieve documents from a database table

We are currently evaluating DocIO for use in an ASP.NET application. We need the following capabilities and would like to know if and how the product can provide them.

1. Save a Doc file into a SQL Table column. We currently do this with custom code and a VarBinary column data type. The file will be uploaded via the browser.

2. Retrieve a document from a SQL table column into a DocIO document object.

3. Ability to update predefined Book Marks within the document with data supplied via program variables.

4. Send the document to the user’s browser and have it open directly in MS Word application.

NOTE: All of the above needs to be done without the need to access the file system of our web application server.

7 Replies

DK Dhivya K Syncfusion Team November 2, 2006 02:43 PM UTC

Hi Scott,

Thank you for your interest in Essential DocIO.

We do have support for saving and retrieving the word document from a database without accessing fileserver using memory stream.
I will provide you with a sample by tomorrow.

Regards,
Dhivya.


DK Dhivya K Syncfusion Team November 6, 2006 01:20 PM UTC

Hi Scott,

Sorry for the delay in getting back to you.

Here are some code snippet for doing the following:
1. Save a Doc file into a SQL Table column.

//Open the file as stream
Stream Sample = new MemoryStream();
Sample = new FileStream(Server.MapPath("sample.doc"), FileMode.Open, FileAccess.ReadWrite);

//store the bytes in a array
byte[] aDocByteArray = new Byte[Sample.Length];
Sample.Read(aDocByteArray, 0, System.Convert.ToInt32(Sample.Length));
Sample.Seek(0, SeekOrigin.Begin);

//Save it to database as binary
SqlParameter myParameter = new SqlParameter("@doc", SqlDbType.VarBinary, aDocByteArray.Length);
myParameter.Value = aPdfDocByteArray;
mCommand.Parameters.Add(myParameter);
mCommand.ExecuteNonQuery();

2. Retrieve a document from a SQL table column into a DocIO document object.
//get the doc from the database
byte[] DocByteArray = (byte[])myCommand.ExecuteScalar();

//Create a FileStream
FileStream fs = new FileStream("Sample.doc", FileMode.Create);
fs.Write(DocByteArray, 0, System.Convert.ToInt32(DocByteArray.Length));
fs.Seek(0, SeekOrigin.Begin);


3. Ability to update predefined Book Marks within the document with data supplied via program variables.
//open the document from database
WordDocument ldoc = new WordDocument(OpenDatabase(Conn));

//Add some bookmarks
AddBookmarks(ldoc);

4. Send the document to the user’s browser and have it open directly in MS Word application.
// Creating a memory Stream.
MemoryStream ms = new MemoryStream();

// Save the pdf document to the Stream.
ldoc.Save(ms,FormatType.Doc);
Response.OutputStream.Write(formSample.GetBuffer(), 0, formSample.GetBuffer().Length);

Currently we do not have support for opening a word document as stream.Hence you have to save the document as file when you retrieve the document from database[in step 2].I will consult with development team regarding this feature and send you an update as soon as I hear back.

Here is a sample for your reference:
SaveToDatabase.zip

Please let me know if you have any other queries.

Regards,
Dhivya.


DK Dhivya K Syncfusion Team November 6, 2006 02:36 PM UTC

Hi Scott,

Regarding the 2nd step[save to file],you can save to memory while retrieving and open it as stream using DocIO which allows to do all the operarions without accessing file server.[Sorry for the confusion!]

Here is the code snippet.
MemoryStream ms = new MemoryStream();
ms.Write(DocByteArray, 0, System.Convert.ToInt32(DocByteArray.Length));
ms.Seek(0, SeekOrigin.Begin);

//open the document from database
WordDocument ldoc = new WordDocument();
ldoc.Open(ms);

Here is the modified sample for your reference
SaveToDatabase.zip

Please let me know if you have any queries.

Regards,
Dhivya.


RA Raja November 24, 2006 11:19 AM UTC

Very Excellent and useful information.

Thanks to all.


DK Dhivya K Syncfusion Team November 24, 2006 02:39 PM UTC

Hi Raja,

Thank you for your interest in Essential DocIO. Glad to know that it helps you. Please let me know if you have any queries.

Regards,
Dhivya.


GM Gabi Münster May 4, 2007 08:44 AM UTC

Hi Dhivya,

sorry, but I can't get the changed file from your second posting. Is it still available?

Regards,
Gabi


BP Bhuvaneswari P Syncfusion Team May 7, 2007 11:55 AM UTC

Hi Gabi,

Sorry for the delay in getting back to you.

Thank you for your interest in Syncfusion products.

Herewith I have attached the sample for both loads the document from Database and save the document into the database.

http://websamples.syncfusion.com//samples/DocIO.Windows/F54716_1/main.htm

Please take a look into this and let me know if you have any other queries.

Best Regards,
Bhuvaneswari.

Loader.
Live Chat Icon For mobile
Up arrow icon