我需要帮助,这是我的代码。我的表格中有一个图片框。但每次我运行程序。我无法从访问中检索图像。而且我有多个图片框因为我需要从其他表中检索所有图像,所以这里是我的代码检索其中一个表的示例。谢谢。
Private Sub p1()
If Not con.State = ConnectionState.Open Then
con.Open()
End If
With aAdapter
.SelectCommand = New OleDb.OleDbCommand()
.SelectCommand.CommandText = "SELECT [president],[prespic] FROM president WHERE presnum= '" & pcount1.Text & "'"
.SelectCommand.Connection = con
End With
Dim dataRead As OleDb.OleDbDataReader
dataRead = aAdapter.SelectCommand.ExecuteReader()
If (dataRead.Read() = True) Then
pres1.Text = (dataRead("president"))
Dim x As Byte() = dataRead("prespic")
Dim ms As MemoryStream = New MemoryStream(x)
Me.prespic1.Image = Image.FromStream(ms)
End If
Com = con.CreateCommand
Dim num As Integer
Com.CommandText = "SELECT COUNT(vote.presnum) from vote where presnum= '" & pcount1.Text & "'"
If IsDBNull(Com.ExecuteScalar) Then
pres1.Visible = False
pnum1.Visible = False
Else
num = Com.ExecuteScalar
pnum1.Text = num
End If
Com.Dispose()
con.Close()
aAdapter.Dispose()
End Sub
这是我保存的代码。
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()
If Not dataRead.HasRows Then
ComInsert.CommandText = "INSERT INTO president([presnum],[president],[prespic])" & _
"VALUES('" & pnum.Text & "','" & prestxt.Text & "','" & "@d7" & "')"
Com.Parameters.AddWithValue("@d7", imgbuffer)
ComInsert.Connection = con
ComInsert.ExecuteNonQuery()
MsgBox("New Candidate Added!", MsgBoxStyle.Information, "NEW RECORD")
prestxt.Text = ""
aAdapter.Dispose()
ComInsert.Dispose()
Com.Dispose()
Call newcount()
Else
MsgBox("WARNING: Candidate Already Exist in the Record!!", MsgBoxStyle.Exclamation, "ERROR SAVING DATA")
End If
答案 0 :(得分:1)
您的INSERT查询已被识别。始终在所有情况下使用参数。您尝试为图像字段使用参数,但最终传递了字符串调用"@d7"
而不是名为@d7
的参数。
ComInsert.CommandText = "INSERT INTO president ([presnum],[president],[prespic])" & _
"VALUES(@pnum, @ptxt, @d7)"
ComInsert.Parameters.AddWithValue("@pnum", pnum.Text)
ComInsert.Parameters.AddWithValue("@ptxt", prestxt.Text)
ComInsert.Parameters.AddWithValue("@d7", imgbuffer)
答案 1 :(得分:0)
请试一试并告诉我它是如何为您服务的?
Dim x As Byte() = DirectCast(dataRead("prespic"), Byte())
Dim ms As New MemoryStream(x)
Me.prespic1.Image = Image.FromStream(ms)
ms.Dispose()
这应该这样做,即如果你将它存储为二进制数据......