2011-05-06 65 views
7

我有這個數據庫,其中有一個表的下面,但我沒有辦法正是DeCypher它存儲的Microsoft Word 97頁中的SQL Server列中的文檔

DATA,  TYPE,   FILE TYPE, SIZE, DOC TYPE 
0x15234324 , Word.Document.8 ,DOC,  19968, WORD.DOCUMENT.8 

領域似乎包含存儲在一個Word文檔SQL Server IMAGE

有沒有人遇到過這個或以可讀的格式提取數據的方法?

到目前爲止,我已經嘗試使用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 
+0

你也許包含在'DATA'字節只是存儲在文件系統和然後用MS Word打開該文件。 – 2011-05-06 12:37:09

+0

很酷 - 如何從SQL服務器提取這些數據?因爲字符串接縫在SQL管理查詢結果窗口中是短的 – Rob 2011-05-06 12:38:48

+3

是的,SSMS將始終截斷爲最大值。長度 - 您需要使用一些代碼從SQL Server中獲取它。看到這個[其他SO問題](http://stackoverflow.com/questions/2818557/are-there-any-utilities-to-extract-binary-data-from-sql-server)上類似的話題 - 也許這'我會幫你 – 2011-05-06 12:39:43

回答

4

我寫了一個VBS腳本來提取數據出來的SharePoint斑點而回,這裏是它的一個通用版本:

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 
+0

我試了上面的和即時得到一個轉換器錯誤在word中,然後下面的文本 - ÿÿÿÿDocumentWord.Document.8 Word.Document.8Ï¡¡>þÿ - 看起來像輸出是錯誤的 – Rob 2011-06-27 11:34:44

+0

我測試了一個Word 2007年的文件存儲在Sharepoint 2007數據庫中,它似乎工作正常。 – Dave 2011-06-27 14:41:27

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或任何你使用的。

+0

看到更新2上面這是我撕了沙發和即時獲取索引越界的代碼!有任何想法嗎? – Rob 2011-06-23 13:01:16

0

嘗試這樣的事情,換成你自己的「一些」值:

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 
+0

這個工作和文件寫出來,但它dosnt在word中打開得到一個轉換器錯誤在word中,然後下面的文本 - ÿÿÿÿ文件Word.Document.8 Word.Document.8æà¡>þÿ - 看起來像輸出是錯誤的 – Rob 2011-06-27 12:38:26

0

我想你會在下面的行代碼中得到IndexOutOfRangeException:

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

因爲第一個參數的索引是0而不是1

無論如何,我建議你使用像Klark所建議的那樣的方法,它更簡單,更具可讀性。

相關問題