[英]Storing html content and other documents (pdf, word, excel) in sql server?
[英]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.