2011-12-29 37 views
1

我有「文件流」啓用數據庫。我創建了具有「file-stream」屬性的列的表,並設法在表中成功記錄行。所以,我得到的是作爲BLOB存儲在我的「文件流」中的圖像。訪問文件流數據方法(T-SQL和託管API)

我的問題是什麼?

我一定要得到這個圖像,並顯示在使用傳統的ASP(我在這個服務器語言總的新手,我不允許使用asp.net)的Web瀏覽器。我已經搜索和閱讀了很多(關於用asp.net做這些事情的信息很多,幾乎沒有任何信息顯示如何用傳統的asp做到這一點),並發現了一篇文章(http://www.simple-talk.com/sql/learn-sql-server/an-introduction-to-sql-server-filestream/),它顯示了讀取數據的方式:

「使用TSQL訪問FILESTREAM數據」和「使用託管API訪問FILESTREAM數據」

第一個我已經能夠理解和使用的第一個。第二個(有一個例子與vb.net代碼)我一直未能。

這是代碼:

‘Create a connection to the database 

Dim ConStr As String 

ConStr = "Data Source=JACOBXPS\katmaifs;Initial Catalog=NorthPole" & _ 

    ";Integrated Security=True" 

Dim con As New SqlConnection(ConStr) 

con.Open() 



'Retrieve the FilePath() of the image file 

Dim sqlCommand As New SqlCommand() 

sqlCommand.Connection = con 

sqlCommand.CommandText = "SELECT ItemImage.PathName() AS PathName " + _ 

        "FROM items WHERE ItemNumber = 'MS1001'" 

Dim filePath As String = sqlCommand.ExecuteScalar() 



'Obtain a Transaction Context 

Dim transaction As SqlTransaction = con.BeginTransaction("ItemTran") 

sqlCommand.Transaction = transaction 

sqlCommand.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()" 

Dim txContext As Byte() = sqlCommand.ExecuteScalar() 



' Open and read file using SqlFileStream Class 

Dim sqlFileStream As New SqlFileStream(filePath, txContext, FileAccess.Read) 

Dim buffer As Byte() = New Byte(sqlFileStream.Length) {} 

sqlFileStream.Read(buffer, 0, buffer.Length) 



'Bind the image data to an image control 

Dim ms As MemoryStream = New MemoryStream(buffer) 

Dim bmp As Bitmap = New Bitmap(ms) 

ItemImage.Image = bmp 



'Cleanup 

sqlFileStream.Close() 

sqlCommand.Transaction.Commit() 

con.Close() 

我沒能在傳統的ASP改造這一點,但在同一篇文章中我所讀過的東西更令人沮喪:

使用T-SQL訪問FILESTREAM數據時,SQL Server將讀取> FILESTREAM數據文件的內容並將其提供給客戶端。 SQL Server內存用於讀取>數據文件的內容。使用Win32 Streaming訪問FILESTREAM數據不會使用SQL Server內存。此外,它還允許應用程序利用NT文件系統的> Streaming功能。

那麼,什麼是我真正的問題?

可以是vb.net代碼轉換並用於經典asp嗎?如果可以,這是否意味着當我使用「文件流」啓用期貨並希望在web中顯示數據時,它會比使用桌面應用程序更慢?

我很困惑,歡迎任何答覆或鏈接與文章閱讀。

回答

2

對這個問題有點困惑,但如果你正在試圖做的一切都顯示從數據庫中的一些BLOB數據在網頁上的圖片,那麼你應該有一個「image.asp」頁面,看起來是這樣的。 ..

SQL = "SELECT FILE_NAME,IMAGE_FILE FROM tblImages WHERE ID = " & request("id") 
Set rs =db.Execute(SQL) 
if not(rs.eof) then 

    Response.ContentType = "application/octet-stream" 
    Response.AddHeader "Content-Disposition", "attachment;filename=" & rs("FILE_NAME") 
    Response.BinaryWrite rs("IMAGE_FILE") 
else 
    response.Write("No such record found in the database at row " & request("id")) 
end if