vb2010 picturebox控件加载access数据库中的ole图片问题
要求是用一个combobox和picturebox连接access数据库,数据表里面有姓名和照片,照片以ole形式存在数据库中。目的是用combobox选择姓名,pict...
要求是用一个combobox和picturebox连接access数据库,数据表里面有姓名和照片,照片以ole形式存在数据库中。目的是用combobox选择姓名,picturebox中则显示对应的照片,现在combobox直接用数据项绑定姓名数据,但是不会在picturebox中显示,在线等解决方法,最好是具体点。
用的是visual studio 2010 ,vb 展开
用的是visual studio 2010 ,vb 展开
2个回答
展开全部
下面是代码,整个工程见附件
Public Class Form1
Public cn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Data.mdb")
Public img As Byte() '图片处理用的字节数组
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filepath As String = ""
Dim opg As New OpenFileDialog
opg.Filter = "图像文件|*.jpg;*.gif;*.bmp"
If opg.ShowDialog() = Windows.Forms.DialogResult.OK Then
filepath = opg.FileName
Else
Exit Sub
End If
img = My.Computer.FileSystem.ReadAllBytes(filepath) 'filePath是你图片文件的路径
Dim comm As OleDb.OleDbCommand
comm = New OleDb.OleDbCommand( _
"INSERT INTO Photo(姓名,照片) Values('" & Me.ComboBox1.Text.Trim & "',@image)", cn)
'向数据库添加存储了图片数据的二进制数组
comm.Parameters.Add("@image", _
OleDb.OleDbType.Binary, img.Length).Value = img
If cn.State = ConnectionState.Closed Then cn.Open() '打开数据库连接
comm.ExecuteNonQuery() '执行数据库命令
If cn.State = ConnectionState.Open Then cn.Close() '关闭数据库连接
MessageBox.Show("图片成功保存到数据库", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Public Function Data_Search(ByVal str As String) As DataTable '查询函数,返回值为数据表
Try
Dim tb As New DataTable
Dim ap As New OleDb.OleDbDataAdapter(str, cn)
ap.Fill(tb)
Return tb
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return Nothing
End Try
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tb As DataTable = Data_Search("select 姓名 from Photo")
For i = 0 To tb.Rows.Count - 1
Me.ComboBox1.Items.Add(tb.Rows(i).Item(0).ToString)
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.ComboBox1.Text = "" Then Exit Sub
Me.PictureBox1.Image = Nothing
Dim tb As DataTable = Data_Search("select 照片 from Photo where 姓名='" & ComboBox1.Text.Trim & "'")
img = tb.Rows(0).Item(0)
Dim imageBytedata As New System.IO.MemoryStream(img) '图片处理用的字节
Me.PictureBox1.Image = Image.FromStream(imageBytedata)
MessageBox.Show("图片显示成功", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询