简体   繁体   English

将照片从数据库检索到PictureBox

[英]Retrieve photo from database to PictureBox

I have a VB.NET project with 2 WinForms. 我有一个带有2个WinForms的VB.NET项目。 Now in Form 1 , I added a PictureBox, a Button, a OpenFileDialog. 现在在Form 1中 ,我添加了一个PictureBox,一个Button和一个OpenFileDialog。 I have coded the button to add the picture from PictureBox to sql database. 我已经编码了将图片从PictureBox添加到sql数据库的按钮。 Now I have another picturebox in Form 2 . 现在我在Form 2中有另一个PictureBox。

So my question is, how do I retrieve the image from the sql database and show it in the picture box? 所以我的问题是,如何从sql数据库中检索图像并将其显示在图片框中?

The connection string is as follows : 连接字符串如下:

Dim con As New SqlConnection
con.ConnectionString = "Data source=" & My.Settings.sqlserver & "," & My.Settings.sqlport &
                       ";Network Library=DBMSSOCN;initial catalog=" & My.Settings.dbname & 
                       ";User id=" & My.Settings.Username &
                       ";Password=" & My.Settings.Password & ";"
Dim cmd As New SqlCommand("select * from userandadmins where Username = @username and Password = @password", con)
con.Open()

I use Oracle to store photos. 我使用Oracle存储照片。 You need to retrieve the photo with a out parameter that is of the same type as the field the photo is stored in. I assume it's a BLOB. 您需要使用out参数来检索照片,该参数与存储照片的字段的类型相同。我认为这是BLOB。 Before storing the photo in the database you have to convert it to a byte array. 在将照片存储在数据库中之前,必须将其转换为字节数组。 As that wasn't the question here, I'll post the code I use to retrieve the photo from my Oracle database, it should be a simple matter to convert to Sql Server code. 因为这不是这里的问题,所以我将发布用于从Oracle数据库检索照片的代码,将其转换为Sql Server代码应该很简单。

I open a connection to the database and pass that to the retrieve function. 我打开与数据库的连接,并将其传递给检索功能。 I return a dataset that has one row of data, the photo and other identifying information. 我返回的数据集包含一行数据,照片和其他标识信息。 I then assign the BLOB to a variable, from there do what you need. 然后,我将BLOB分配给变量,然后从那里执行所需的操作。 What I do with it is assign it to an image control which calls another aspx page to retrieve the photo (imageURL). 我将其分配给一个图像控件,该控件调用另一个aspx页面以检索照片(imageURL)。 The code to retrieve it is in the called "Image.aspx", which then returns the photo to the image control. 检索它的代码在称为“ Image.aspx”的文件中,然后将照片返回给图像控件。 The html for that is below the GetPhoto Function. 的html在GetPhoto函数下面。 This way I do not need to store the photo anywhere on the hard drive and retrieve it. 这样,我无需将照片存储在硬盘驱动器上的任何位置并进行检索。

Dim MyConnection As New OracleConnection
Dim MyDataSet As New DataSet
Dim MyPhoto() As Byte = {}

MyConnection = OpenConnection(Session("USERNAME"), Session("PASSWORD"))

MyDataSet = GetPhoto(MyConnection, pPhotoID)

myPhoto = MyDataSet.Tables("Data").Rows(0)("Photo")

Public Function GetPhoto(ByVal TheConnection As OracleConnection, ByVal pPhotoID As String) As DataSet
    Dim myCommand As New OracleCommand
    Dim DS As New DataSet
    Try
        With myCommand
            .Connection = TheConnection
            .CommandText = "Darlington.PlayerSignUps.GetPhoto"
            .CommandType = CommandType.StoredProcedure
            .Parameters.Clear()
            .Parameters.Add(New OracleParameter("pPhotoID", OracleDbType.Varchar2, pPlayerID.Length)).Value = pPhotoID 
            .Parameters.Add(New OracleParameter("pDataOut", OracleDbType.RefCursor))
            .Parameters(0).Direction = ParameterDirection.Input
            .Parameters(1).Direction = ParameterDirection.Output
            Dim DA As New OracleDataAdapter(myCommand)
            DA.Fill(DS, "DATA")
            GetPhoto = DS
        End With
    Catch exc As Exception
        Throw New Exception("Error occured while retreiving photo from database, the error is: " & exc.Message)
    End Try
    myCommand = Nothing

End Function

<tr>
    <td style="width: 372px; text-align: center">
        <asp:Image ID="Image_Photo" runat="server" ImageUrl="Image.aspx" />
    </td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM