从访问数据库中检索图片 [英] Retrieve picture from access database

查看:49
本文介绍了从访问数据库中检索图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图片保存在 access 数据库中,一切正常,但是当我想检索时却无法检索.

I'm saving a picture in an access database, and everything is OK but when I want to retrieve it I can't.

这是我将图片保存在数据库中的代码.我需要代码将其检索到 vb.net 中的 PictureBox

This is my code to save the picture in database. I need code to retrieve it to a PictureBox in vb.net

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     Try

        OpenFileDialog1.Filter = "image file (*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png| all files (*.*) | *.* "
         If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
             PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
             PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

         End If
     Catch ex As Exception

     End Try
 End Sub

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
     Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)

     Dim breader As New BinaryReader(fsreader)

     Dim imgbuffer(fsreader.Length) As Byte
     breader.Read(imgbuffer, 0, fsreader.Length)
     fsreader.Close()

     con.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\test.accdb;"
     con.Open()
     Dim sql As String
     sql = "insert into TS Values(" & TextBox1.Text & ",'" & imgbuffer.Length & "')"
     Dim cmd As New OleDbCommand(sql, con)
     cmd.ExecuteNonQuery()
     cmd.Dispose()
     con.Close()
 End Sub

推荐答案

但我不认为您将图像保存为二进制格式,甚至您还没有对此查询进行最后更新.所以我假设您已经以二进制形式保存了图像.
我们来看看.

But I don't think that you're saving the Image in a Binary format, and even you have not put your last update on this query. So I assume you have saved the Image in a Binary form.
Let's take a look.

我假设如果您将图像保存在长二进制数据"中,那么您必须使用图像 ID 调用该图像.或者,更好的是,如果您使用的是学生编号.
现在你在这里做的只是制作一个 TextBox(即 Textbox1).然后按照代码.

I assume that if you've saved the Image in "Long Binary data", then you have to call that Image by using the Image ID. Or, better, if you're using a Student's Number.
Now what you do here is simply make a TextBox (i.e. Textbox1). Then follow the code.

先打开一个连接然后:

Open a Connection first then:

Try
    Dim command As New OleDbCommand("SELECT Image FROM Table WHERE Rollno ='" & textbox1.Text & "'", myconnection)
    command.Parameters.AddWithValue("@Rollno", Textbox1.Text)
    Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())

    myconnection.Close()
    command.Dispose()
    Dim stream As New IO.MemoryStream(pictureData)
    Me.PictureBox1.Image = Image.FromStream(stream)'See in this picturebox1 you're showing the Image.
    '' NOTE: don't dispose the stream!!
Catch ex As Exception
    Label3.ForeColor = Color.Red
    Label3.Text = "The Given Rollno has not found in the database"
    Return
End Try

这篇关于从访问数据库中检索图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆