2013-07-18 101 views
0

進口System.Data.OleDb更新查詢是不工作在VB.NET與MS訪問

公共類Form1中

這是我的連接字符串

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|datadirectory|STORE.accdb") 

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click 
    con.Open() 

    Dim cmd As New OleDbCommand("SELECT  [QTY OTHER], [HQTY PL3], [HQTY PL5], [TOTAL QTY] FROM(MATERIAL)WHERE  ([MAT CODE] = @a)", con) 


    cmd.Parameters.AddWithValue("@a", TextBox1.Text) 
    Dim dr As OleDbDataReader = cmd.ExecuteReader 
    Dim qtyotherm, hqtypl3m, hqtypl5m, totalqtym As New Double 
    Dim qtyotherr, rqtypl3r, rqtypl5r, qtyr As New Double 
    Dim totalqtyfinal As New Double 

    Dim cmdins As New OleDbCommand("INSERT INTO RECEPT ([MAT CODE], QTY, RATE, TAXES, [RQTY PL3], [RQTY PL5], [RQTY OTHER]) VALUES  (@b, @c, @d, @e, @f, @g, @h)", con) 


    cmdins.Parameters.AddWithValue("@b", TextBox1.Text) 
    cmdins.Parameters.AddWithValue("@c", TextBox2.Text) 
    cmdins.Parameters.AddWithValue("@d", TextBox10.Text) 
    cmdins.Parameters.AddWithValue("@e", TextBox9.Text) 
    cmdins.Parameters.AddWithValue("@f", TextBox3.Text) 
    cmdins.Parameters.AddWithValue("@g", TextBox4.Text) 
    cmdins.Parameters.AddWithValue("@h", TextBox5.Text) 
    Dim checkinsert As New Integer 
    checkinsert = cmdins.ExecuteNonQuery 

此執行非查詢工作正常

If checkinsert > 0 Then 




     If dr.HasRows Then 


      While dr.Read 
       qtyotherm = dr(0) 
       hqtypl3m = dr(1) 
       hqtypl5m = dr(2) 
       totalqtym = dr(3) 
      End While 

文本框是表示ADDITION VALE

  totalqtyfinal = totalqtym + TextBox2.Text 
      TextBox15.Text = totalqtyfinal 



      MessageBox.Show("chk text box value") 

問題由此處開始

  Dim cmdupdate As New OleDbCommand("UPDATE MATERIAL SET [TOTAL QTY] = @j WHERE ([MAT CODE] = @i)", con) 


      cmdupdate.Parameters.AddWithValue("@i", TextBox1.Text) 
      cmdupdate.Parameters.AddWithValue("@j", TextBox15.Text) 
      Dim chkupdate As New Integer 
      chkupdate = cmdupdate.ExecuteNonQuery 


      If chkupdate > 0 Then 
       MessageBox.Show("update done :) ") 

ALWAYS THIS else語句MESSAGE被示

  Else 
       MessageBox.Show("fail to update :(") 
      End If 




      MessageBox.Show("quer 1 and 2 is successfully executed") 

     Else 
      MessageBox.Show("dr not working.") 
     End If 
    Else 
     MessageBox.Show("insert query fail") 
    End If 
    con.Close() 

End Sub 

末級

回答

1

請參閱this answer。 OleDbCommand不支持命名參數。您需要按照SQL語句中的順序添加參數。

在您的更新聲明中,您的@j之後是@i,並且您正在添加@i,然後是@j。 OleDbCommand有效地將此視爲?其次是 ?在SQL語句中,當您添加參數時,它僅添加參數1,後跟參數2.