我正在開發一個web應用程序,讓用戶有機會以pdf格式上傳他的簡歷。我使用NHibernate作爲數據映射器和MS SQL SERVER 2005.如何保存PDF文件使用NHibernate和SQL Server 2005
我希望能夠將.pdf文件保存到給定的表...任何想法?
非常感謝!
我正在開發一個web應用程序,讓用戶有機會以pdf格式上傳他的簡歷。我使用NHibernate作爲數據映射器和MS SQL SERVER 2005.如何保存PDF文件使用NHibernate和SQL Server 2005
我希望能夠將.pdf文件保存到給定的表...任何想法?
非常感謝!
我們正在用「原始」Java Hibernate3來完成這項工作。您只需將持久化類的字節數組屬性映射到類型爲「image」的列。
package com.hibernate.pdf.sample;
public class TPDFDocument implements java.io.Serializable {
private Integer pdfDocumentId;
private byte[] document;
public Integer getPdfDocumentId() {
return this.pdfDocumentId;
}
public void setPdfDocumentId(Integer pdfDocumentId) {
this.pdfDocumentId = pdfDocumentId;
}
public byte[] getDocument() {
return this.document;
}
public void setDocument(byte[] document) {
this.document = document;
}
}
Hibernate映射:
<hibernate-mapping>
<class name="com.hibernate.pdf.sample.TPDFDocument" table="T_PDFDocument">
<id name="pdfDocumentId" type="integer">
<column name="pdfDocumentId" />
<generator class="identity" />
</id>
<property name="document" type="binary">
<column name="document" not-null="true" />
</property>
</class>
</hibernate-mapping>
創建表:
CREATE TABLE [dbo].[T_PDFDocument](
[pdfDocumentId] [int] IDENTITY(1,1) NOT NULL,
[document] [image] NOT NULL,
CONSTRAINT [PK_PDFDocument] PRIMARY KEY CLUSTERED
(
[pdfDocumentId] ASC
)
所有你需要做的是閱讀文件原始字節到數組中,並堅持它。在我們的情況下,這些文檔幾乎不會超過1MB,因此將整個內容放入字節數組中不會導致性能問題。也許這個解決方案不適用於非常大的文檔。
我想用NHibernate的實現和C#的解決方案看起來非常相似。
INSERT INTO表(DocumentId,PDF) VALUES(NEWID(),0x12345667)
在十六進制常數是PDF的十六進制編碼的字節流。
我確定有人可以找到更好的方法,但這對我來說很明顯。
您需要將PDF放入表格的任何原因?您可以將文件保存到文件系統並使用ID引用它。這將爲您節省一些SQL Server空間。我不相信你可以索引二進制數據。