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

时间:2011-05-06 11:14:06

标签: c# sql-server vb.net visual-studio ms-word

我有一个包含以下内容的数据库,但是我没有办法解密它

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, 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

5 个答案:

答案 0 :(得分:4)

我写了一个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

答案 1 :(得分:2)

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或其他任何使用中都应该不难。

答案 2 :(得分:0)

看一下这个帖子。

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

该伙伴使用SCRIPTING.FILESYSTEMOBJECT从IMAGE列中提取内容并将其写入文件。

通读评论。

答案 3 :(得分:0)

尝试这样的方法,用你自己的代替“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

答案 4 :(得分:0)

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

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

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

无论如何,我建议你使用像Klark建议的方法,它更简单,更易读。