当前位置: 动力学知识库 > 问答 > 编程问答 >

oledb - Updating Image on database MSAccess Database Vb.net?

问题描述:

I want to update images in the database but when i press the button it won't update , I don't have any errors on my code. I added some code in "com.executequery" block to try if i get errors, and i get messagebox result "Error"

 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 [email protected], [email protected], [email protected], [Birthdate][email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [Age][email protected], [Telnum][email protected], [Mobilenum1][email protected], [Mobilenum2][email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [Image][email protected] WHERE [StudentID][email protected] "

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

网友答案:

The fact that the ExecuteNonQuery call succeeds but returns zero means that there were no records in your database that match your WHERE clause. The reason is that you're adding your parameters incorrectly.

Both the Jet and ACE OLE DB providers use positional parameters only. Even though you can give your parameters names, those names are ignored by the provider. That means that you need to add your parameters to your OleDbCommand in the same order as they appear in the SQL code. You are not.

In your SQL code, @fid is the last parameter but this is the last parameter you add to the command:

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

That is therefore the value being used to find matching records. Rearrange the code that adds parameters to the command and add @fid last and you'll be good to go.

分享给朋友:
您可能感兴趣的文章:
随机阅读: