参数异常未处理'参数无效'
问题描述:
你好vb.net相对较新(我使用的是vb.net 2008专门的视觉工作室而不是c#)
i已经能够将图像保存到访问数据库中了但我的问题是能够从数据库中检索图像(使用Access 2003)
这里是我的保存代码
hello there am relatively new to vb.net(i am using vb.net 2008 specifically visual studio not c#)
i have been able to save images into an access database but my problem is to be able to retrieve the images from the database(using access 2003)
here is my saving code
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If inc <> -1 Then
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
Dim ms As New MemoryStream()
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
picPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
con.Open()
sql = "INSERT INTO CONTACTS(AGENTNUMBER,FIRSTNAME,MIDDLENAME,SURNAME,GENDER,EMAIL,PHONE,ADDRESS,NOTES,PICTURE)"
sql = sql & "VALUES(@AGENTNUMBER,@FIRSTNAME,@MIDDLENAME,@SURNAME,@GENDER,@EMAIL,@PHONE,@ADDRESS,@NOTES,@PICTURE)"
cmd.CommandText = sql
cmd.Connection = con
cmd.Parameters.AddWithValue("@AGENTNUMBER", txtAgentNumber.Text)
cmd.Parameters.AddWithValue("@FIRSTNAME", txtFirstName.Text)
cmd.Parameters.AddWithValue("@MIDDLENAME", txtMiddleName.Text)
cmd.Parameters.AddWithValue("@SURNAME", txtSurName.Text)
cmd.Parameters.AddWithValue("@GENDER", cboGender.Text)
cmd.Parameters.AddWithValue("@EMAIL", txtEmail.Text)
cmd.Parameters.AddWithValue("@PHONE", txtPhone.Text)
cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text)
cmd.Parameters.AddWithValue("@NOTES", txtNotes.Text)
cmd.Parameters.AddWithValue("@PICTURE", ms.ToArray())
cmd.ExecuteNonQuery()
cmd.Dispose()
MsgBox("NEW RECORD ADDED")
ds.AcceptChanges()
btnSave.Enabled = False
btnAdd.Enabled = True
btnEdit.Enabled = True
btnDelete.Enabled = True
End If
End Sub
here is my code for retrieving the image
Sub retrieve()
Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PEOPLE.mdb")
con.Open()
Using cmd As New OleDbCommand("SELECT PICTURE FROM CONTACTS WHERE AGENTNUMBER='" & txtAgentNumber.Text & "'", con)
Dim imageobj = cmd.ExecuteScalar
If Not imageobj Is Nothing AndAlso Not imageobj Is DBNull.Value Then
Using ms As New System.IO.MemoryStream
Dim bm As Bitmap
Dim byteArray = DirectCast(imageobj, Byte())
ms.Write(byteArray, 0, byteArray.Length - 1)
bm = New Bitmap(ms)
picPhoto.Image = bm ---- here is where the problem is ((parameter not valid))
End Using
End If
End Using
End Using
End Sub
i搜索了解决方案,但我没有得到它
i已经过了过去1个月在这里掖着
i have googled for the solution but i haven't gotten it
i have been stuck here for the past 1 month
答
我不相信你正在写它是正确的:你从磁盘上读取一个文件(大约复杂的方式) (尽可能)然后完全忽略它,支持目前在picPhoto.Image中发生的任何事情...
如果要保存磁盘文件进入数据库,抛弃这一批:
I'm not convinced you are writing it correctly at all: you read a file from teh disk (in about as complicated way as possible) and then ignore it completely, in favor of whatever happens to be in the picPhoto.Image at present...
If you want to save the disk file into teh DB, throw away this lot:
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
Dim ms As New MemoryStream()
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
picPhoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
并将其替换为:
And replace it with:
Dim data As Byte() = File.ReadAllBytes(OpenFileDialog1.FileName)
然后保存:
Then save it with:
cmd.Parameters.AddWithValue("@PICTURE", data)
试试看你的数据库数据是否更好。请记住,任何现有的值都是垃圾!
Try that and see if your DB data is any better. Remember that any existing values will be rubbish!