ทำให้เป็นอนุกรมและทำงานกับเอกสารในฐานข้อมูล
งานที่คุณอาจต้องทำให้เสร็จเมื่อทำงานกับเอกสารคือการจัดเก็บและเรียกข้อมูลออบเจ็กต์ Document เข้าและออกจากฐานข้อมูล ตัวอย่างเช่น สิ่งนี้จะจำเป็นหากคุณใช้ระบบการจัดการเนื้อหาประเภทใดก็ตาม เอกสารเวอร์ชันก่อนหน้าทั้งหมดจะต้องเก็บไว้ในระบบฐานข้อมูล ความสามารถในการจัดเก็บเอกสารในฐานข้อมูลยังมีประโยชน์อย่างยิ่งเมื่อแอปพลิเคชันของคุณให้บริการบนเว็บ
Aspose.Words ให้ความสามารถในการแปลงเอกสารเป็นอาร์เรย์ไบต์สำหรับการทำงานกับเอกสารนี้ในฐานข้อมูลในภายหลัง
แปลงเอกสารเป็นไบต์อาร์เรย์
ในการจัดเก็บเอกสารในฐานข้อมูลหรือเพื่อเตรียมเอกสารสำหรับการส่งผ่านเว็บ มักจะจำเป็นต้องทำให้เอกสารเป็นอนุกรมเพื่อรับอาร์เรย์ไบต์
หากต้องการทำให้วัตถุ Document เป็นอนุกรมใน Aspose.Words ให้ทำดังนี้
- บันทึกลงใน MemoryStream โดยใช้วิธี Save โอเวอร์โหลดของคลาส Document
- เรียกใช้เมธอด ToArray ซึ่งส่งคืนอาร์เรย์ไบต์ที่แสดงถึงเอกสารในรูปแบบไบต์
ขั้นตอนข้างต้นสามารถย้อนกลับได้เพื่อโหลดไบต์กลับเข้าไปในออบเจ็กต์ Document
ตัวอย่างด้านล่างแสดงวิธีทำให้วัตถุเป็นอนุกรม Document เพื่อรับอาร์เรย์ไบต์ และวิธีการยกเลิกซีเรียลไลซ์อาร์เรย์ไบต์เพื่อรับวัตถุ Document อีกครั้ง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_LoadingAndSaving(); | |
// Load the document from disk. | |
Document doc = new Document(dataDir + "Test File (doc).doc"); | |
// Create a new memory stream. | |
MemoryStream outStream = new MemoryStream(); | |
// Save the document to stream. | |
doc.Save(outStream, SaveFormat.Docx); | |
// Convert the document to byte form. | |
byte[] docBytes = outStream.ToArray(); | |
// The bytes are now ready to be stored/transmitted. | |
// Now reverse the steps to load the bytes back into a document object. | |
MemoryStream inStream = new MemoryStream(docBytes); | |
// Load the stream into a new document object. | |
Document loadDoc = new Document(inStream); |
คุณสามารถดาวน์โหลดไฟล์เทมเพลตของตัวอย่างนี้ได้จาก Aspose.Words GitHub
จัดเก็บ อ่าน และลบเอกสารในฐานข้อมูล
ส่วนนี้จะแสดงวิธีการบันทึกเอกสารในฐานข้อมูล จากนั้นโหลดกลับเข้าไปในออบเจ็กต์ Document
เพื่อใช้งานกับเอกสารนั้น เพื่อความง่าย ชื่อไฟล์คือคีย์ที่ใช้ในการจัดเก็บและดึงเอกสารจากฐานข้อมูล ฐานข้อมูลประกอบด้วยสองคอลัมน์ คอลัมน์แรก “ชื่อไฟล์” จะถูกจัดเก็บเป็นสตริงและใช้เพื่อระบุเอกสาร คอลัมน์ที่สอง “FileContent” ถูกจัดเก็บเป็นวัตถุ BLOB
ซึ่งจัดเก็บวัตถุเอกสารในรูปแบบไบต์
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าการเชื่อมต่อกับฐานข้อมูลและดำเนินการคำสั่ง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
string dbName = ""; | |
// Open a database connection. | |
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + RunExamples.GetDataDir_Database() + dbName; | |
OleDbConnection mConnection = new OleDbConnection(connString); | |
mConnection.Open(); |
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการบันทึกเอกสารลงในฐานข้อมูล จากนั้นอ่านเอกสารเดิมอีกครั้ง และสุดท้ายจะลบบันทึกที่มีเอกสารออกจากฐานข้อมูล:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Store the document to the database. | |
StoreToDatabase(doc, mConnection); | |
// Read the document from the database and store the file to disk. | |
Document dbDoc = ReadFromDatabase(fileName, mConnection); | |
// Save the retrieved document to disk. | |
string newFileName = Path.GetFileNameWithoutExtension(fileName) + " from DB" + Path.GetExtension(fileName); | |
dbDoc.Save(dataDir + newFileName); | |
// Delete the document from the database. | |
DeleteFromDatabase(fileName, mConnection); | |
// Close the connection to the database. | |
mConnection.Close(); |
บันทึกเอกสารลงในฐานข้อมูล
เมื่อต้องการบันทึกเอกสารในฐานข้อมูล ให้แปลงเอกสารนี้เป็นอาร์เรย์ไบต์ ดังที่อธิบายไว้ตอนต้นของบทความนี้ จากนั้นให้บันทึกอาร์เรย์ไบต์นี้ลงในช่องฐานข้อมูล
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการบันทึกเอกสารลงในฐานข้อมูลที่ระบุ:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void StoreToDatabase(Document doc, OleDbConnection mConnection) | |
{ | |
// Save the document to a MemoryStream object. | |
MemoryStream stream = new MemoryStream(); | |
doc.Save(stream, SaveFormat.Docx); | |
// Get the filename from the document. | |
string fileName = Path.GetFileName(doc.OriginalFileName); | |
// Create the SQL command. | |
string commandString = "INSERT INTO Documents (FileName, FileContent) VALUES('" + fileName + "', @Docx)"; | |
OleDbCommand command = new OleDbCommand(commandString, mConnection); | |
// Add the @Docx parameter. | |
command.Parameters.AddWithValue("Docx", stream.ToArray()); | |
// Write the document to the database. | |
command.ExecuteNonQuery(); | |
} |
ระบุ commandString ซึ่งเป็นนิพจน์ SQL ที่ทำงานได้ทั้งหมด:
- หากต้องการบันทึกเอกสารลงในฐานข้อมูล จะใช้คำสั่ง “INSERT INTO” และระบุตารางพร้อมกับค่าของฟิลด์บันทึกสองฟิลด์ ได้แก่ FileName และ FileContent เพื่อหลีกเลี่ยงพารามิเตอร์เพิ่มเติม ชื่อไฟล์จะนำมาจากออบเจ็กต์ Document เอง ค่าฟิลด์
FileContent
ถูกกำหนดเป็นไบต์จากสตรีมหน่วยความจำ ซึ่งมีการแสดงไบนารีของเอกสารที่เก็บไว้ - บรรทัดโค้ดที่เหลือดำเนินการคำสั่งซึ่งจัดเก็บเอกสาร Aspose.Words ในฐานข้อมูล
ดึงเอกสารจากฐานข้อมูล
หากต้องการดึงเอกสารจากฐานข้อมูล ให้เลือกเรกคอร์ดที่มีข้อมูลเอกสารเป็นอาร์เรย์ไบต์ จากนั้นโหลดอาร์เรย์ไบต์จากบันทึกลงใน MemoryStream และสร้างออบเจ็กต์ Document ที่จะโหลดเอกสารจาก MemoryStream
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการดึงและส่งคืนเอกสารจากฐานข้อมูลที่ระบุโดยใช้ชื่อไฟล์เป็นคีย์ในการดึงเอกสารนี้:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static Document ReadFromDatabase(string fileName, OleDbConnection mConnection) | |
{ | |
// Create the SQL command. | |
string commandString = "SELECT * FROM Documents WHERE FileName='" + fileName + "'"; | |
OleDbCommand command = new OleDbCommand(commandString, mConnection); | |
// Create the data adapter. | |
OleDbDataAdapter adapter = new OleDbDataAdapter(command); | |
// Fill the results from the database into a DataTable. | |
DataTable dataTable = new DataTable(); | |
adapter.Fill(dataTable); | |
// Check there was a matching record found from the database and throw an exception if no record was found. | |
if (dataTable.Rows.Count == 0) | |
throw new ArgumentException(string.Format("Could not find any record matching the document \"{0}\" in the database.", fileName)); | |
// The document is stored in byte form in the FileContent column. | |
// Retrieve these bytes of the first matching record to a new buffer. | |
byte[] buffer = (byte[])dataTable.Rows[0]["FileContent"]; | |
// Wrap the bytes from the buffer into a new MemoryStream object. | |
MemoryStream newStream = new MemoryStream(buffer); | |
// Read the document from the stream. | |
Document doc = new Document(newStream); | |
// Return the retrieved document. | |
return doc; | |
} |
ลบเอกสารออกจากฐานข้อมูล
หากต้องการลบเอกสารออกจากฐานข้อมูล ให้ใช้คำสั่ง SQL ที่เหมาะสมโดยไม่มีการเปลี่ยนแปลงใดๆ กับออบเจ็กต์ Document
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบเอกสารออกจากฐานข้อมูล โดยใช้ชื่อไฟล์เพื่อดึงข้อมูลบันทึก:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void DeleteFromDatabase(string fileName, OleDbConnection mConnection) | |
{ | |
// Create the SQL command. | |
string commandString = "DELETE * FROM Documents WHERE FileName='" + fileName + "'"; | |
OleDbCommand command = new OleDbCommand(commandString, mConnection); | |
// Delete the record. | |
command.ExecuteNonQuery(); | |
} |