database 在 vb.net 中从 sql 数据库显示/检索图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2413476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
display/retrieve image from sql database in vb.net
提问by ferdd
This should be pretty simple for a pro. I have images in sql server database and I want to retrieve them in my aspx (vb.net) file. I have in aspx this image control - in vb.net i have started this code -
对于专业人士来说,这应该很简单。我在 sql server 数据库中有图像,我想在我的 aspx (vb.net) 文件中检索它们。我在 aspx 中有这个图像控件 - 在 vb.net 中我已经启动了这段代码 -
Private Sub ImageDisplay()
Dim SqlCnn As SqlConnection = Nothing, sql As String = ""
ConnectDB(SqlCnn)
Try
sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
sqlcmd = New SqlCommand(sqlstr, SqlCnn)
Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
Dim newImage As Image = Nothing
If Not imageData Is Nothing Then
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
newImage = Image.FromStream(ms, True)
End Using
image1.Image = newImage
End If
Catch ex As Exception
ReportError(ex)
Finally
CloseDB(SqlCnn)
End Try
End Sub
I am getting error on 2 places. - newImage = Image.FromStream(ms, True) - image1.Image = newImage Fromstream is not a member of system.web.ui.webcontrols.image and second error Image is not a member of system.web.ui.webcontrols.image
我在 2 个地方出错。- newImage = Image.FromStream(ms, True) - image1.Image = newImage Fromstream 不是 system.web.ui.webcontrols.image 的成员,第二个错误 Image 不是 system.web.ui.webcontrols.image 的成员
回答by Guffa
You need both a command object and a data reader. However, you are putting them in the wrong page.
您需要一个命令对象和一个数据读取器。但是,您将它们放在错误的页面中。
If you check out the properties of the Image
control you will see that it doesn't have any Image
property, so you can't load an image and put in the control. The reason for that is that you can't send both the page and the image in the same response, instead the browser has to request the image separately when the page loads.
如果您查看Image
控件的属性,您将看到它没有任何Image
属性,因此您无法加载图像并放入控件中。这样做的原因是你不能在同一个响应中同时发送页面和图像,相反,浏览器必须在页面加载时单独请求图像。
To get the image from the database and show in the web page you need a separate proxy page that you can use to get only the image data from the database. In the ImageUrl
property of the Image
control you would put something like "GetImage.ashx"
. Then you make an HTTP handler with that name that will just get the image data from the database and write to the response stream.
要从数据库中获取图像并显示在网页中,您需要一个单独的代理页面,您可以使用它来仅从数据库中获取图像数据。在控件的ImageUrl
属性中,Image
您将放置类似"GetImage.ashx"
. 然后使用该名称创建一个 HTTP 处理程序,该处理程序将从数据库中获取图像数据并写入响应流。
Example:
例子:
Private Sub HandleRequest(context as HttpContext)
Dim SqlCnn As SqlConnection = Nothing, sql As String
Dim emp_id As Integer
emp_id = Int32.Parse(context.Request.QueryString("id"))
ConnectDB(SqlCnn)
Try
sql = "SELECT image FROM employees (NOLOCK) WHERE ID =" & emp_id
sqlcmd = New SqlCommand(sqlstr, SqlCnn)
Dim imageData As Byte() = DirectCast(sqlcmd.ExecuteScalar(), Byte())
context.Response.ContentType = "image/jpeg"
context.Response.BinaryWrite(imageData)
Catch ex As Exception
ReportError(ex)
Finally
CloseDB(SqlCnn)
End Try
End Sub
回答by Vijay Raval
Dim cn As SqlConnection
cn = New SqlConnection
cn.ConnectionString = "Data Source=UMAR\UMAR;Initial Catalog=DMCHS;Integrated Security=True"
Dim cmd As New System.Data.SqlClient.SqlCommand("select D1 from DBFile where mem_no=2")
cmd.Connection = cn
cmd.CommandType = CommandType.Text
Dim ImgStream As New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte()))
PictureBox1.Image = Image.FromStream(ImgStream)
ImgStream.Dispose()
cmd.Connection.Close()