0

我已成功在 sql server 数据库中输入图像。现在我想从数据库中检索此图像并将其显示在图像控件中。我不想在页面上放置任何 ListBox。我在 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 行中

4

1 回答 1

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 页面的情况下,您只能在来自 Url 的网页上显示图像,首先应该有一个图片网页,因为 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

///

于 2012-04-05T06:46:54.023 回答