c# – 在SQL Server列中存储Microsoft Word 97文档

前端之家收集整理的这篇文章主要介绍了c# – 在SQL Server列中存储Microsoft Word 97文档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个具有以下表格的数据库,但我没有办法解密它
DATA,TYPE,FILE TYPE,SIZE,DOC TYPE
0x15234324,Word.Document.8,DOC,19968,WORD.DOCUMENT.8

该字段似乎包含存储在sql Server IMAGE列中的word文档

有没有人遇到这个或以可读格式提取这些数据?

到目前为止,我已经尝试使用PHP提取文件并将其写入word文档,但没有太多运气.

更新:我现在有Visual Studio Express,想要一种提取这些数据并保存到word文档的方法

UPDATE2:这是我在VB sofar中所拥有的

Imports System.Data.sqlClient
Imports System.IO


Public Class Form1

    Private Shared Function RetrieveFile(ByVal filename As String) As Byte()
        Dim connection As New sqlConnection("Server=sqlsrv;database=database;Trusted_Connection=Yes;")
        Dim command As New sqlCommand("select data from objects where object_ref in (select data from parts where object_ref =239804)",connection)
        command.Parameters.AddWithValue("test",filename)
        connection.Open()
        Dim reader As sqlDataReader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
        reader.Read()
        Dim memory As New MemoryStream()
        Dim startIndex As Long = 0
        Const ChunkSize As Integer = 256
        While True
            Dim buffer As Byte() = New Byte(ChunkSize - 1) {}
            Dim retrievedBytes As Long = reader.GetBytes(1,startIndex,buffer,ChunkSize)
            memory.Write(buffer,CInt(retrievedBytes))
            startIndex += retrievedBytes
            If retrievedBytes <> ChunkSize Then
                Exit While
            End If
        End While
        connection.Close()
        Dim data As Byte() = memory.ToArray()
        memory.Dispose()
        Return data


    End Function


    Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "Doc File|*.doc"
        saveFileDialog1.Title = "Save an doc File"
        saveFileDialog1.ShowDialog()

        If saveFileDialog1.FileName <> "" Then
            Dim fs As New System.IO.FileStream(saveFileDialog1.FileName,System.IO.FileMode.Create,System.IO.FileAccess.Write)
            Dim data As Byte() = RetrieveFile("test.doc")
            fs.Write(data,data.Length)
            fs.Flush()
            fs.Close()
        End If
    End Sub




End Class

解决方法

我写了一个VBS脚本,暂时从sharepoint blob中提取数据,这是它的通用版本:
Const adOpenKeyset                  = 1
Const adLockOptimistic              = 3
Const adTypeBinary                  = 1
Const adSaveCreateOverWrite         = 2

strsqlServer = "YOURSERVER"
strsqlDatabase = "YOURDB"
strRecordID = "123"
strTempFileName = "c:\output.doc"

Set objConn = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.RecordSet")
Set objStream = CreateObject("ADODB.Stream")

objConn.Open "Provider=sqlOLEDB;data Source=" & strsqlServer & ";Initial Catalog=" & strsqlDatabase & "; Trusted_Connection=yes;"
objRS.Open "Select * from AllDocStreams WHERE ID='" & strRecordID & "'",objConn,adOpenKeyset,adLockOptimistic

objStream.Type = adTypeBinary
objStream.Open
objStream.Write objRS.Fields("Content").Value
objStream.SaveToFile strTempFileName,adSaveCreateOverWrite

objRS.Close
objConn.Close
原文链接:https://www.f2er.com/csharp/100224.html

猜你在找的C#相关文章