在数据库MSAccess数据库Vb.net上更新映像?

时间:2014-09-21 18:30:12

标签: vb.net oledb jet

我想要更新数据库中的图像,但是当我按下按钮它不会更新时,我的代码上没有任何错误。我在" com.executequery"中添加了一些代码。阻止尝试,如果我得到错误,我得到消息框结果"错误"

 Private Sub updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updatebtn.Click
            If Agetxt.SelectedItem = Nothing Or gendertxt.SelectedItem = Nothing Or Yrlvltxt.SelectedItem = Nothing Or PictureBox1.Image Is Nothing Then

                MsgBox("Please do not leave required fields blanks.", vbExclamation, "Warning!")
            Else
                Dim memstream As New MemoryStream
                Dim datapic_update As Byte()
                Me.PictureBox1.Image.Save(memstream, Imaging.ImageFormat.Jpeg)
                datapic_update = memstream.GetBuffer()
                memstream.Read(datapic_update, 0, memstream.Length)

'to check if connection is open
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If

                'Updating DB
                Dim editQ As String = "Update Infos SET FirstName=@f1, SurName=@f2, MiddleName=@f3, [Birthdate]=@f4, Gender=@f5, HomeAddress=@f6, CityAddress=@f7, BaranggayAddress=@f8, EmailAdd1=@f9, Birthplace=@f10, Yearlevel=@f11, Course=@f12, Emailadd2=@f13, [Age]=@f14, [Telnum]=@f15, [Mobilenum1]=@f16, [Mobilenum2]=@f17, FathersName=@f18, FathersL=@f19, MothersName=@f20, MothersL=@f21, FathersOcc=@f22, MothersOcc=@f23, StreetAdd=@f24, [Image]=@Image WHERE [StudentID]=@fid "
                Dim com As New OleDbCommand(editQ, con)
                con.Open()


                com.Parameters.AddWithValue("@fid", Stdntid.Text.ToString)
                com.Parameters.AddWithValue("@f1", fname.Text)
                com.Parameters.AddWithValue("@f2", Sname.Text)
                com.Parameters.AddWithValue("@f3", Mname.Text)
                com.Parameters.AddWithValue("@f4", Datetxt.Value.ToShortDateString)
                com.Parameters.AddWithValue("@f5", gendertxt.SelectedItem.ToString)
                com.Parameters.AddWithValue("@f6", homaddtxt.Text)
                com.Parameters.AddWithValue("@f7", Cityadd.Text)
                com.Parameters.AddWithValue("@f8", brgyadd.Text)
                com.Parameters.AddWithValue("@f9", emailaddtxt.Text)
                com.Parameters.AddWithValue("@f10", birthPtxt.Text)
                com.Parameters.AddWithValue("@f11", Yrlvltxt.SelectedItem.ToString)
                com.Parameters.AddWithValue("@f12", coursetxt.Text)
                com.Parameters.AddWithValue("@f13", emailadd2txt.Text)
                com.Parameters.AddWithValue("@f14", Agetxt.SelectedItem.ToString)
                com.Parameters.AddWithValue("@f15", telnumtxt.Text)
                com.Parameters.AddWithValue("@f16", mobilenum1txt.Text)
                com.Parameters.AddWithValue("@f17", mobilenum2txt.Text)
                com.Parameters.AddWithValue("@f18", FathersL.Text)
                com.Parameters.AddWithValue("@f19", fatherstxt.Text)
                com.Parameters.AddWithValue("@f20", MothersL.Text)
                com.Parameters.AddWithValue("@f21", motherstxt.Text)
                com.Parameters.AddWithValue("@f22", fOcc.Text)
                com.Parameters.AddWithValue("@f23", mOcc.Text)
                com.Parameters.AddWithValue("@f24", streetadd.Text)


                ' image content
                Dim image As OleDbParameter = New OleDbParameter("@Image", SqlDbType.Image)
                image.Value = datapic_update
                com.Parameters.Add(Image)


                com.ExecuteNonQuery()
                If com.ExecuteNonQuery > 0 Then
                    MsgBox("Records Successfully Updated.", vbInformation, "Updated.")
                Else
                    MsgBox("error")
                End If



            End If
            con.Close()



        End Sub

1 个答案:

答案 0 :(得分:0)

ExecuteNonQuery调用成功但返回零的事实意味着数据库中没有与WHERE子句匹配的记录。原因是您错误地添加了参数。

Jet和ACE OLE DB提供程序仅使用位置参数。即使您可以提供参数名称,提供程序也会忽略这些名称。这意味着您需要以与SQL代码中显示的顺序相同的顺序将参数添加到OleDbCommand。你不是。

在您的SQL代码中,@fid是最后一个参数,但这是您添加到命令的最后一个参数:

com.Parameters.AddWithValue("@f24", streetadd.Text)

因此,这是用于查找匹配记录的值。重新排列为命令添加参数的代码,最后添加@fid,你就可以了。