我已成功在sql server数据库中输入了一个图像。现在我想从数据库中检索此图像并在Image控件中显示它。我不想在页面上放任何ListBoxes。我在ASP dot Net写道。
Dim MS As MemoryStream
Dim IMG As System.Drawing.Image
Dim FS As FileStream
Dim FI As FileInfo
Dim IMGSTRM As Stream
Dim IMGBYTS As Byte()
Dim ImgData() As Byte
CMD1 = New SqlCommand("select * from IMG where userid=0", CON1)
If Not IsNothing(RDR1) Then RDR1.Close()
RDR1 = CMD1.ExecuteReader()
If RDR1.Read Then
IMGBYTS = RDR1!img
MS = New MemoryStream(IMGBYTS, False)
IMG = Image.FromStream(MS)
PIC1.ImageUrl = IMG
End If
问题在于End If
上面的粗体2行答案 0 :(得分:1)
如果您使用的是桌面应用程序,则这是解决方案
Private Sub SqlBlob2File(ByVal DestFilePath As String)
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
Dim cmd As New SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim ms As New System.IO.MemoryStream(b)
Me.PictureBox1.Image = System.Drawing.Image.FromStream(ms)
End Sub
这是您可能想要查看的链接:http://support.microsoft.com/kb/321900/en-us
asp.net解决方案
如果是网页或aspx页面,你只能在网页上显示一个网页上的图像,首先应该是一个图片网页,因为只有一个图片框的webform2应该在那里。此页面是webform1的图像。
通过这种方式,您可以根据需要在页面上放置尽可能多的图片 当然你制作了相同数量的伪页面(并且不给它们 同名)。
我希望这将是一个不错的页面。
\\ webform1上必须有一个图像框,一个按钮和一个标签
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlConnection(connStr)
Dim cmd As New SqlCommand("SELECT FileName, PictureID FROM Picture", conn)
da = New SqlDataAdapter(cmd)
cbd = New SqlCommandBuilder(da)
dsPictures = New DataSet
da.Fill(dsPictures)
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
ListBox1.DataSource = dsPictures.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlException
Me.Label1.Text = "Database Error" 'sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = "Datbase Connection Failed!"
End Try
conn.Close()
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/testSQLPlaatjesWeb/WebForm2.aspx"
End Sub
/// \\
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection(connStr)
Dim sqlstr As String = String.Format("SELECT Picture FROM Picture WHERE
(PictureID = {0})", CInt(Session.Item("img")))
Dim cmd As New SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Response.BinaryWrite(CType(rdr.Item("Picture"), Byte()))
rdr.Close()
conn.Close()
End Sub
///