2014-09-21 55 views
-1

我想更新數據庫中的圖像,但是當我按下按鈕時它不會更新,我的代碼沒有任何錯誤。我添加了一些代碼在「com.executequery」塊嘗試,如果我得到的錯誤,我也得到消息框,結果「錯誤」更新數據庫MSAccess數據庫Vb.net上的圖像?

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 
+0

可能重複的[如何使用vb.net和adodb連接在mysql數據庫中插入圖像](http://stackoverflow.com/questions/24924982/how-to-insert-image-in-mysql-database-using -vb-net-and-adodb-connection)這裏的71,000之一 – Plutonix 2014-09-21 19:29:36

+0

已經試過,但仍然不會工作 – MichaelXX 2014-09-21 20:03:44

+0

我仍然收到相同的錯誤 – MichaelXX 2014-09-21 20:08:19

回答

0

ExecuteNonQuery調用成功,但返回零意味着沒有記錄的事實您的數據庫符合您的WHERE條款。原因是你錯誤地添加了你的參數。

Jet和ACE OLE DB提供程序都只使用位置參數。即使您可以提供參數名稱,這些名稱也會被提供程序忽略。這意味着您需要按照它們在SQL代碼中出現的順序將您的參數添加到您的OleDbCommand。你不是。

在你的SQL代碼,@fid是最後一個參數,但是這是你添加到命令的最後一個參數:

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

因此正在使用的值來查找匹配記錄。重新安排將參數添加到命令的代碼,並添加@fid,最後你會很好。

+0

謝謝你現在正在工作。圖片內容被屏蔽後,我重新排列了@fid – MichaelXX 2014-09-22 03:41:03