【问题标题】:Insert a doc file into a column in SQL Server在 SQL Server 的列中插入 doc 文件
【发布时间】:2016-03-04 21:40:43
【问题描述】:

我想在 SQL Server 的一个字段中保存一个 doc 文件。该 doc 文件可以包含文本、图表、表格和图片。

我想将此文件保存在一个字段中,并且我可以随时在这些 doc 文件中搜索并找到我的搜索标题。

我该怎么做?

【问题讨论】:

标签: c# asp.net sql-server sql-server-2008


【解决方案1】:

在您的表中创建一个数据类型为varbinary(max) 的列,然后尝试-

string filePath = FileUpload1.PostedFile.FileName;  
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
 switch(ext)
{
    case ".doc":
        contenttype = "application/vnd.ms-word";
        break;
    case ".docx":
        contenttype = "application/vnd.ms-word";
        break;
}
 if (contenttype != String.Empty)
{

    Stream fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

    //insert the file into database
    string strQuery = "insert into tblFiles(Name, ContentType, Data)" +
       " values (@Name, @ContentType, @Data)";
    SqlCommand cmd = new SqlCommand(strQuery);
    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
    cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
      = contenttype;
    cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
    InsertUpdateData(cmd);
    lblMessage.ForeColor = System.Drawing.Color.Green;  
    lblMessage.Text = "File Uploaded Successfully";
}

【讨论】:

    【解决方案2】:

    您可以在 SQL 中创建一个新的数据库字段,类型为VARBINARY(MAX),它可以保存任何数据。为了将 Doc 文件保存到其中,我将帮助您举个例子。

            // Read the file and convert it to Byte Array
    
            string filePath = FileUpload1.PostedFile.FileName;  
            string filename = Path.GetFileName(filePath);
    
            Stream fs = FileUpload1.PostedFile.InputStream;
    
            BinaryReader br = new BinaryReader(fs);
    
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    
    
    
            //insert the file into database
    
            string strQuery = "insert into tblFiles(Name, Data)" +
    
               " values (@Name, @Data)";
    
            SqlCommand cmd = new SqlCommand(strQuery);
    
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
    
    
    
            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
    
            InsertUpdateData(cmd);
    
            lblMessage.ForeColor = System.Drawing.Color.Green;  
    
            lblMessage.Text = "File Uploaded Successfully";
    

    这里的字段DATA在SQL中定义为VarBinary(max)

    对于您的第二个要求,要在 DOC 中搜索文本,请参考Configure and Manage Filters for Search

    【讨论】:

      【解决方案3】:

      感谢我的朋友 但我希望能够在我的 doc 文件中进行搜索。 doc 文件可以包含表格,表格可以包含任何类型的数据,例如文本、数字和 ....
      图表和图片也可以像表格一样。
      而且我不想将我的文档的路径保存在一个字段中,并且任何时候我需要先搜索我打开文件然后搜索。
      我想将该 doc 文件保存到一个字段中并在该字段上搜索

      【讨论】:

        猜你喜欢
        • 2013-06-08
        • 2013-06-07
        • 2010-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        相关资源
        最近更新 更多