错误:参数无效 [英] Error: Parameter not valid
问题描述
导入 System.Data.OleDb
导入 System.IO
导入 System.Drawing.Imaging
公共 类检索
私有 子 Button1_Click( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 Button1.Click
Dim conn = 新建 OleDbConnection(" Provider = Microsoft.ACE.OLEDB.12.0;"和_
" &应用程序.启动路径" )
conn.Open()
Dim Str As 字符串
Str = " & TextBox1.Text& "
Dim cmmd = 新 OleDbCommand(Str,conn)
Dim dr As OleDbDataReader
博士= cmmd.ExecuteReader()
如果博士HasRows 然后 ' 程序的其余部分
Dim RetVal As 长
Dim FieldLen As Int32
Dim AccessConnection As 新建 OleDbConnection(" & _
" &应用程序.启动路径" )
Dim AccessCommand As 新建 OleDbCommand(" & TextBox1.Text& " '",AccessConnection)
AccessConnection.Open()
Dim AccessDataReader As OleDbDataReader = AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
AccessDataReader.Read()
FieldLen = AccessDataReader.Item(" ).长度
Dim BinaryByteArray(FieldLen- 1 ) As 字节
Dim StartIndex As 整数 = 0
RetVal = AccessDataReader.GetBytes( 1 ,StartIndex,BinaryByteArray, 0 ,BinaryByteArray.Length)
Dim OLEStream As New System.IO. MemoryStream(BinaryByteArray)
PictureBox1.Image = Image.FromStream(OLEStream)
AccessDataReader.Close()
AccessConnection.Close()
其他
MsgBox(" )
结束 如果
结束 子
结束 类
大家好,我正在尝试从MS Access中检索图片.
但我在这部分上遇到错误:
PictureBox1.Image = Image.FromStream(OLEStream)->参数无效.
我已经尝试调试了很长时间.. im一遍又一遍地得到相同的错误:p
请帮助我...谢谢..
您需要查看两件事:数据库中的数据以及如何将其存储在数据库中,以及您已读出的数据.
将字节转换为图像的代码看起来不错,因此数据一定有问题.
尝试使用较小的图像(专门制作一个图像,例如8 x 8像素),然后看看会得到什么.
我已经按照你的意思去做了,
Dim bytImage() As 字节 尝试 Dim ms As New System.IO.内存流 Dim bmpImage As 新位图(PictureBox1.图像) bmpImage.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg) bytImage = ms.ToArray() ms.Close() 捕获,例如 As 异常 MsgBox(例如ToString) 结束 尝试
然后将其保存以访问:
SQL = " & UserName& ; "
我的检索代码:
与上述相同.
我仍然遇到相同的错误.
您处在正确的位置(尽管您无需将其调暗为新的位图-您可以使用现有的位图).
暗 bmpImage As 新位图(PictureBox1.Image) bmpImage.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg)成为
PictureBox1.Image.Save(毫秒,System.Drawing.Imaging.ImageFormat.Jpeg)
您需要做的就是更改将其保存到数据库的方式(您不能仅将字节作为文本字符串的一部分传递-数据库将尝试将它们解释为命令并失败)
使用 con As 新 SqlConnection(strConnect) con.Open() 使用 com 使用 新建 SqlCommand(" ,同上 com.Parameters.AddWithValue(" ,bytImage) com.ExecuteNonQuery() 结束 使用 结束 使用您可能需要使用OleDbConnection和OleDbCommand而不是SqlConnection和SqlCommand
Imports System.Data.OleDb
Imports System.IO
Imports System.Drawing.Imaging
Public Class Retrieve
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Application.StartupPath & "\DBASE.accdb;")
conn.Open()
Dim Str As String
Str = "SELECT Signature FROM DB WHERE Username = '" & TextBox1.Text & "'"
Dim cmmd = New OleDbCommand(Str, conn)
Dim dr As OleDbDataReader
dr = cmmd.ExecuteReader()
If dr.HasRows Then 'REST OF THE PROGRAM
Dim RetVal As Long
Dim FieldLen As Int32
Dim AccessConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Application.StartupPath & "\AMADS.accdb")
Dim AccessCommand As New OleDbCommand("SELECT Signature FROM DB WHERE [UserName] = '" & TextBox1.Text & "'", AccessConnection)
AccessConnection.Open()
Dim AccessDataReader As OleDbDataReader = AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
AccessDataReader.Read()
FieldLen = AccessDataReader.Item("Signature").Length
Dim BinaryByteArray(FieldLen - 1) As Byte
Dim StartIndex As Integer = 0
RetVal = AccessDataReader.GetBytes(1, StartIndex, BinaryByteArray, 0, BinaryByteArray.Length)
Dim OLEStream As New System.IO.MemoryStream(BinaryByteArray)
PictureBox1.Image = Image.FromStream(OLEStream)
AccessDataReader.Close()
AccessConnection.Close()
Else
MsgBox("...")
End If
End Sub
End Class
Hi guys, i am trying to retrieve picture from MS access.
but i am getting an error on this part:
PictureBox1.Image = Image.FromStream(OLEStream) --> parameter not valid.
i''ve tried debugging this for ages.. im getting the same error again and again :p
Help me please... thanks..
You need to look at two things: the data that you have in the DB and how you stored it in there, and the data that you have read out.
The code to convert the bytes to an image looks good, so it has to be a problem with the data.
Try it with a small image (make one specially, say 8 by 8 pixels) and see what you get.
Hi, ive tried to do what you said, here it goes:
Dim bytImage() As Byte Try Dim ms As New System.IO.MemoryStream Dim bmpImage As New Bitmap(PictureBox1.Image) bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) bytImage = ms.ToArray() ms.Close() Catch ex As Exception MsgBox(ex.ToString) End Try
then to save it to access:
SQL = "INSERT INTO DB VALUES ('" & UserName & "', 'bytImage' )"
my code for retrieving:
the same as above.
im still getting the same error.
You are on the right lines (although you don''t need to dim it as a new bitmap - you can use the existing one).
Dim bmpImage As New Bitmap(PictureBox1.Image) bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)Becomes
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
All you need to do is change the way you save it to the DB (You can''t just pass the bytes as part of a text string - the DB will try to interpret them as a command and fail)
Using con As New SqlConnection(strConnect) con.Open() Using com As New SqlCommand("INSERT INTO myTable (nameOfImageColumnInDB) VALUES (@IMAGE)", con) com.Parameters.AddWithValue("@IMAGE", bytImage) com.ExecuteNonQuery() End Using End UsingYou may need to use OleDbConnection and OleDbCommand instead of SqlConnection and SqlCommand
这篇关于错误:参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!