访问文件流数据方法(T-SQL 和托管 API)

发布于 2024-12-23 07:04:40 字数 2284 浏览 0 评论 0 原文

我有启用“文件流”的数据库。我创建了包含具有“文件流”属性的列的表,并设法成功记录表中的行。所以,我得到的是作为 BLOB 存储在我的“文件流”中的图像。

我的问题是什么?

我必须获取此图像并使用经典 asp 在 Web 浏览器中显示它(我是该服务器语言的新手,并且不允许我使用 asp.net)。我已经搜索并阅读了很多内容(有很多关于使用 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 中转换它,但在同一篇文章中我读到了一些更令人沮丧的内容:

当使用 TSQL 访问 FILESTREAM 数据时,SQL Server 读取 >FILESTREAM 数据文件的内容并将其提供给客户端。 SQL Server 内存用于读取数据文件的内容。使用 Win32 Streaming 访问 FILESTREAM 数据不使用 SQL Server 内存。此外,它还允许应用程序利用 NT 文件系统的流功能。

那么我真正的问题是什么?

vb.net 代码可以转换并在经典的 asp 中使用吗?如果可以,这是否意味着当我使用“文件流”启用 futures 并希望在 Web 中显示数据时,速度会更慢使用桌面应用程序?

我很困惑,非常感谢任何答案或与要阅读的文章的链接。

I have "File-Stream" enabled database. I have created table with column that has the "file-stream" attribute and manage to record successfully row in the table. So, what I have got is image stored in my "File-Stream" as BLOB.

What is my problem?

I have to get this image and show it in the web browser using classic asp (I am total newbie in this server-language and I am not allowed to use the asp.net). I have searched and read a lot (there are many information about doing that with asp.net and almost nothing showing how to do this with classic asp) and found a article (http://www.simple-talk.com/sql/learn-sql-server/an-introduction-to-sql-server-filestream/) that shows to ways to read the data:

"Accessing FILESTREAM Data using TSQL" and "Accessing FILESTREAM data with Managed API"

The first one I have been able to understand and use. The second one (there is a example with vb.net code) I haven't been able.

This is the code:

‘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()

I was not able to transform this in classic asp but in the same article I have read something more frustrating:

When accessing FILESTREAM data using TSQL, SQL Server reads the content of the >FILESTREAM data file and serves it to the client. SQL Server memory is used for reading >the content of the data file. Accessing FILESTREAM data using Win32 Streaming does not >use SQL Server memory. In addition it allows the application to take advantage of the >Streaming capabilities of the NT File System.

So what is my real problem?

Can be the vb.net code transform and used in a classic asp?And if it can, does that mean that when I am using the "file-stream" enable futures and want to display data in the web it will be more slow then using desktop application?

I am pretty confused and will appreciate any answer or link with article to read.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-12-30 07:04:40

对这个问题有点困惑,但如果您想要做的只是将数据库中的一些 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

A little confused on the question but if all you're trying to do is display some BLOB data from the database as an Image on the webpage then you should have an "image.asp" page that looks something like this ...

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文