我在访问数据库中保存了一张图片,一切正常,但是当我想要检索它时,我不能。
这是我将图片保存在数据库中的代码。我需要代码将其检索到vb.net中的PictureBox
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
答案 0 :(得分:0)
但我不认为您是以二进制格式保存图像,甚至您还没有将此最新更新放在此查询上。所以我假设你已经以二进制形式保存了图像 我们来看看。
我假设如果您已将图像保存在“长二进制数据”中,则必须使用图像ID
来调用该图像。或者,更好的是,如果您使用学生编号
现在你在这里做的只是制作一个TextBox
(即Textbox1)。然后按照代码。
首先打开一个连接然后:
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