插入&通过图片框检索图片 [英] Insert & retrieve Picture through picturebox

查看:79
本文介绍了插入&通过图片框检索图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个图片框和一个用于浏览图片的按钮.

保存一个btn,搜索一个btn.

如何将图片保存在ms访问表中?
如何从访问表中搜索图片?

Hi folks,

I have one picturebox and one button for browse picture.

One btn for save and one btn for search.

How can I save picture in ms access table?
How can I search picture from access table?

推荐答案

首先,声明您的访问表.您将需要一个Id字段,我将使用ReplicationID,但您可以使用自动编号.
添加一个名为image的字段,并将其声明为Ole Object
然后添加代码:
Firstly, declare your Access Table. You will need an Id field, I would use ReplicationID, but you can use autonumber.
Add a field called image, and declare it as Ole Object
Then add code:
Private Sub LoadImage(iD As Guid)
	Using con As New OleDbConnection(strConnection)
		con.Open()
		Using com As New OleDbCommand("SELECT image FROM myTable where id=@ID", con)
			com.Parameters.AddWithValue("@ID", iD)
			Dim reader As OleDbDataReader = com.ExecuteReader()
			If reader.Read() Then
				Dim data As Byte() = DirectCast(reader(0), Byte())
				Dim stream As New MemoryStream(data)
				myPictureBox.Image = Image.FromStream(stream)
			End If
		End Using
	End Using
End Sub
Private Function SaveImage() As System.Nullable(Of Guid)
	Dim image As New Bitmap(myPictureBox.Image)
	Dim stream As New MemoryStream()
	image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
	Dim data As Byte() = stream.ToArray()
	Dim iD As System.Nullable(Of Guid) = Guid.NewGuid()
	Using con As New OleDbConnection(strConnection)
		con.Open()
		Try
			Using com As New OleDbCommand("INSERT INTO myTable (iD, image) VALUES (@ID, @IM)", con)
				com.Parameters.AddWithValue("@ID", iD)
				com.Parameters.AddWithValue("@IM", data)
				com.ExecuteNonQuery()
			End Using
		Catch ex As Exception
			MessageBox.Show(ex.Message.ToString())
			MessageBox.Show(ex.StackTrace.ToString())
			iD = Nothing
		End Try
	End Using
	Return iD
End Function


See Here[^]

Google is your friend!


这篇关于插入&通过图片框检索图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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