繁体   English   中英

在“ SQL Server”列中存储Microsoft Word 97文档

[英]Storing Microsoft Word 97 documents in SQL Server column

我有一个带有以下内容的表的数据库,但无法解密

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 沙发上所拥有的

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, 0, ChunkSize)
            memory.Write(buffer, 0, 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, 0, data.Length)
            fs.Flush()
            fs.Close()
        End If
    End Sub




End Class

我写了一个VBS脚本,将数据从共享点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

C#代码:

connection.Open();
SqlCommand command1 = new SqlCommand("select DATA from TABLE where ...", connection);
byte[] img = (byte[])command1.ExecuteScalar();
File.WriteAllBytes("your_path/word.doc", img);

这应该是逻辑。 用您知道的任何语言写类似的东西。 在PHP或您正在使用的任何工具中都不难。

看一下这个线程。

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=101754

家伙使用SCRIPTING.FILESYSTEMOBJECT进行操作,以从IMAGE列中提取内容并将其写入文件。

阅读评论。

尝试类似的事情,用您自己的替换“ some”值:

declare @doc varbinary(max), @ObjectToken int

select @doc = (select data from yourTable were someID = @idThatYouWant)

set @FileName = '\someFolder\' + 'someFilename.doc'

print 'Processing: ' + isnull(@FileName, 'null')

exec sp_oacreate 'ADODB.Stream', @objecttoken output
exec sp_oasetproperty @objecttoken, 'type', 1
exec sp_oamethod @objecttoken, 'open'
exec sp_oamethod @objecttoken, 'write', null, @doc
exec sp_oamethod @objecttoken, 'savetofile', null, @FileName, 2
exec sp_oamethod @objecttoken, 'close'
exec sp_oadestroy @objecttoken

我认为您将在代码的以下行中获得IndexOutOfRangeException:

 Dim retrievedBytes As Long = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize)

因为第一个参数的索引是0而不是1

无论如何,我建议您使用一种类似于Klark所建议的方法,该方法更简单易读。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM