سریال سازی و کار با یک سند در یک پایگاه داده
یکی از کارهایی که ممکن است لازم باشد هنگام کار با اسناد انجام دهید، ذخیره و بازیابی اشیاء Document به و از پایگاه داده است. به عنوان مثال، اگر شما هر نوع سیستم مدیریت محتوا را پیاده سازی می کنید، این امر ضروری است. تمام نسخه های قبلی اسناد باید در سیستم پایگاه داده ذخیره شوند. توانایی ذخیره اسناد در پایگاه داده نیز زمانی که برنامه شما یک سرویس مبتنی بر وب ارائه می دهد بسیار مفید است.
Aspose.Words توانایی تبدیل یک سند به آرایه بایتی را برای کار بعدی با این سند در پایگاه داده فراهم می کند.
تبدیل یک سند به آرایه بایت
برای ذخیره یک سند در یک پایگاه داده یا آماده سازی یک سند برای انتقال در سراسر وب، اغلب لازم است که سند را سریالی کنید تا یک آرایه بایتی بدست آورید.
برای سریال کردن یک شی Document در Aspose.Words:
- با استفاده از روش Save اضافه بار کلاس Document، آن را در یک MemoryStream ذخیره کنید.
- متد 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
برای کار با آن نشان می دهد. برای سادگی، نام فایل کلیدی است که برای ذخیره و واکشی اسناد از پایگاه داده استفاده می شود. پایگاه داده شامل دو ستون است. ستون اول “FileName” به عنوان یک رشته ذخیره می شود و برای شناسایی اسناد استفاده می شود. ستون دوم “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(); | |
} |