0

我已经访问了数据库中的数据并在文本框中显示了一些信息,但是如何从文件路径中显示图像?

aspx:

<asp:Image ID="PlaceEventImage" runat="server" Height="120px" Width="217px" />

aspx.vb:

            Dim placeName As String = CType(Session.Item("Place"), String)
            Dim address1 As String = CType(Session.Item("Address1"), String)
            Dim countyID As Integer = CType(Session.Item("CountyID"), Integer)

            Dim aConnection4 As New OleDbConnection
            Dim aDataAdapter5 As OleDbDataAdapter
            Dim ds1 As New DataSet
            Dim aCommand5 As OleDbCommand
            Dim Sql5 As String
            aConnection4.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data/MyLocalWebsite.mdb")
            Sql5 = "SELECT PlaceName, Address1, Address2, PlaceData.CountyID, PlaceCategoryID, Image, CountyData.CountyID, County FROM PlaceData INNER JOIN CountyData ON (CountyData.CountyID = PlaceData.CountyID) WHERE PlaceName = '" & placeName & "' AND Address1 = '" & address1 & "' ORDER BY PlaceName"
            aCommand5 = New OleDbCommand(Sql5, aConnection4)

            aConnection4.Open()

            aDataAdapter5 = New OleDbDataAdapter(Sql5, aConnection4)
            aDataAdapter5.Fill(ds1, "PlaceInfo")

            aConnection4.Close()

            Dim imageUrl As String = ds1.Tables("PlaceInfo").Rows(0).Item(5)

            PlaceEventName.Text = ds1.Tables("PlaceInfo").Rows(0).Item(0)
            Address1Label.Text = ds1.Tables("PlaceInfo").Rows(0).Item(1)
            Address2Label.Text = ds1.Tables("PlaceInfo").Rows(0).Item(2)
            CountyLabel.Text = ds1.Tables("PlaceInfo").Rows(0).Item(7)

最终让它工作,下面是有效的解决方案。图像作为完整文件路径保存到数据库,但图像已上传到服务器,因此需要更改服务器的路径:

Dim imageUrl As String = ds1.Tables("PlaceInfo").Rows(0).Item(5)
Dim found As Integer = imageUrl.LastIndexOf("\")
Dim filename As String = imageUrl.Substring(found)
Dim newFilename As String = filename.Replace("\", "/")
PlaceEventImage.ImageUrl = "~/Uploads" + newFilename
4

0 回答 0