【问题标题】:How to save a PDF file Using NHibernate and SQL Server 2005如何使用 NHibernate 和 SQL Server 2005 保存 PDF 文件
【发布时间】:2008-10-10 20:03:07
【问题描述】:

我正在开发一个网络应用程序,用户可以在其中上传 pdf 格式的简历。我使用 NHibernate 作为数据映射器和 MS SQL SERVER 2005。

我希望能够将 .pdf 文件保存到给定的表格中...有什么想法吗?

非常感谢!

【问题讨论】:

    标签: .net sql-server nhibernate pdf


    【解决方案1】:

    我们使用“原始”Java Hibernate3 正是这样做的。您只需将持久类的字节数组属性映射到“图像”类型的列。

    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-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# 的解决方案看起来非常相似。

    【讨论】:

      【解决方案2】:

      插入表格(DocumentId、PDF) 值 (newid(), 0x12345667)

      其中的十六进制常量是 PDF 的十六进制编码字节流。

      我相信有人能找到更好的方法,但这对我来说是显而易见的。

      【讨论】:

        【解决方案3】:

        您有什么理由需要将 PDF 放入表格中?您可以将文件保存到文件系统并使用 ID 引用它。这将为您节省一些 SQL Server 空间。我不相信你可以索引二进制数据。

        【讨论】:

          猜你喜欢
          • 2010-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 2010-10-09
          • 2011-03-09
          相关资源
          最近更新 更多